简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
2015-11-18 23:01
489 查看
工厂模式也用了不少,特别是MS的petshop中对数据库的访问,通过工厂模式可以达到自由切换SQL 和 Oracle 数据库。近来也在看设计模式的书,发现工厂模式还是有不少的扩展。结合书中的知识和我自己理解,从代码的角度来比较工厂模式各变种。
1:简单工厂模式:其作用是实例化对象而不需要客户了解这个对象属于那个具体的子类。
using System;
using System.Collections;
public class MyClass
{
public static void Main()
{
//通过参数来实例化子类。
IVehicle vehicle = FactoryVehicle.CreateVehicle("car");
vehicle.go();
Console.ReadLine();
}
}
public class FactoryVehicle
{
public static IVehicle CreateVehicle(string VehicleName)
{
switch(VehicleName.ToLower())
{
case "car":
return new Car();
case "boat":
return new Boat();
default:
return new Car();
}
}
}
public interface IVehicle
{
void go();
}
public class Car:IVehicle
{
public void go()
{
Console.WriteLine("car");
}
}
public class Boat:IVehicle
{
public void go()
{
Console.WriteLine("boat");
}
}
2:抽象工厂:即将工厂方法也抽象出来,由具体的子类来实例化工厂。产品创建部分和简单工厂模式相同。
using System;
using System.Collections;
public class MyClass
{
public static void Main()
{
//通过定义的工厂来实例化。弊端是当产品很多时需要增加很多的工厂。代码重复。
FactoryVehicle factory = new FactoryCar();
IVehicle vehicle = factory.CreateVehicle();
vehicle.go();
Console.ReadLine();
}
}
public interface FactoryVehicle
{
IVehicle CreateVehicle();
}
public class FactoryCar:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Car();
}
}
public class FactoryBoat:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Boat();
}
}
public interface IVehicle
{
void go();
}
public class Car:IVehicle
{
public void go()
{
Console.WriteLine("car");
}
}
public class Boat:IVehicle
{
public void go()
{
Console.WriteLine("boat");
}
}
3:反射工厂模式: 其实就是通过反射的方式来获得具体实例化是那个类。
using System;
using System.Collections;
using System.Reflection;
public class MyClass
{
public static void Main()
{
//使用反射的方法获得实例化的类
IVehicle vechicle = ReflectFactory.CreateVehicleByReflect("Car");
vechicle.go();
Console.ReadLine();
}
}
public class ReflectFactory
{
public static IVehicle CreateVehicleByReflect(string typeName)
{
Type type = Type.GetType(typeName);
ConstructorInfo ci = type.GetConstructor(System.Type.EmptyTypes);;
return (IVehicle)ci.Invoke(null);
}
}
public class FactoryBoat:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Boat();
}
}
public class FactoryCar:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Car();
}
}
public interface FactoryVehicle
{
IVehicle CreateVehicle();
}
public interface IVehicle
{
void go();
}
public class Car:IVehicle
{
public void go()
{
Console.WriteLine("car");
}
}
public class Boat:IVehicle
{
public void go()
{
Console.WriteLine("boat");
}
}
1:简单工厂模式:其作用是实例化对象而不需要客户了解这个对象属于那个具体的子类。
using System;
using System.Collections;
public class MyClass
{
public static void Main()
{
//通过参数来实例化子类。
IVehicle vehicle = FactoryVehicle.CreateVehicle("car");
vehicle.go();
Console.ReadLine();
}
}
public class FactoryVehicle
{
public static IVehicle CreateVehicle(string VehicleName)
{
switch(VehicleName.ToLower())
{
case "car":
return new Car();
case "boat":
return new Boat();
default:
return new Car();
}
}
}
public interface IVehicle
{
void go();
}
public class Car:IVehicle
{
public void go()
{
Console.WriteLine("car");
}
}
public class Boat:IVehicle
{
public void go()
{
Console.WriteLine("boat");
}
}
2:抽象工厂:即将工厂方法也抽象出来,由具体的子类来实例化工厂。产品创建部分和简单工厂模式相同。
using System;
using System.Collections;
public class MyClass
{
public static void Main()
{
//通过定义的工厂来实例化。弊端是当产品很多时需要增加很多的工厂。代码重复。
FactoryVehicle factory = new FactoryCar();
IVehicle vehicle = factory.CreateVehicle();
vehicle.go();
Console.ReadLine();
}
}
public interface FactoryVehicle
{
IVehicle CreateVehicle();
}
public class FactoryCar:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Car();
}
}
public class FactoryBoat:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Boat();
}
}
public interface IVehicle
{
void go();
}
public class Car:IVehicle
{
public void go()
{
Console.WriteLine("car");
}
}
public class Boat:IVehicle
{
public void go()
{
Console.WriteLine("boat");
}
}
3:反射工厂模式: 其实就是通过反射的方式来获得具体实例化是那个类。
using System;
using System.Collections;
using System.Reflection;
public class MyClass
{
public static void Main()
{
//使用反射的方法获得实例化的类
IVehicle vechicle = ReflectFactory.CreateVehicleByReflect("Car");
vechicle.go();
Console.ReadLine();
}
}
public class ReflectFactory
{
public static IVehicle CreateVehicleByReflect(string typeName)
{
Type type = Type.GetType(typeName);
ConstructorInfo ci = type.GetConstructor(System.Type.EmptyTypes);;
return (IVehicle)ci.Invoke(null);
}
}
public class FactoryBoat:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Boat();
}
}
public class FactoryCar:FactoryVehicle
{
public IVehicle CreateVehicle()
{
return new Car();
}
}
public interface FactoryVehicle
{
IVehicle CreateVehicle();
}
public interface IVehicle
{
void go();
}
public class Car:IVehicle
{
public void go()
{
Console.WriteLine("car");
}
}
public class Boat:IVehicle
{
public void go()
{
Console.WriteLine("boat");
}
}
相关文章推荐
- Java中提供的工具类
- 冒泡排序算法(C#实现)
- [MFC] CDialog::DoModal()函数用法
- 深入理解C#:查询集合
- Python 学习LINK
- 快速排序算法(C#实现)
- Java记录 -82- 静态导入 Static Import
- JAVA注解
- 1. Spring4.1-依赖注入
- java环境的搭建
- 细说 JavaBean
- php中的魔术方法__call(),__clone(),__set(),__get()
- struts返回json
- Visual Studio 2013 打包C#项目程序
- 拓展开源库PullZoomView适配瀑布流
- 杭电ACM1000,1001,1002 java解答
- [亲测]ThinkPHP中where方法中变量不解析的解决方法
- Java transient关键字
- leetcode Roman to Integer python
- c++顺序容器的学习