简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
来源:互联网 发布:算法时代 下载 编辑:程序博客网 时间:2024/05/16 15:42
工厂模式也用了不少,特别是MS的petshop中对数据库的访问,通过工厂模式可以达到自由切换SQL 和 Oracle 数据库。近来也在看设计模式的书,发现工厂模式还是有不少的扩展。结合书中的知识和我自己理解,从代码的角度来比较工厂模式各变种。
1:简单工厂模式:其作用是实例化对象而不需要客户了解这个对象属于那个具体的子类。
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.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");
}
}
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.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");
}
}
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");
}
}
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");
}
}
0 0
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂、抽象工厂和工厂模式的总结
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂&工厂模式&抽象工厂模式
- 抽象工厂模式、简单工厂模式、工厂模式的区别
- 简单工厂模式、工厂模式、抽象工厂模式的区别
- 抽象工厂模式+反射
- 简单工厂模式、工厂方法模式、反射工厂模式、抽象工厂模式
- 简单工厂模式+反射来改进抽象工厂模式
- 抽象工厂模式的简单代码示例
- 简单工厂,工厂模式,抽象工厂
- 简单工厂,工厂模式,抽象工厂
- 简单工厂、工厂方法、抽象工厂模式
- 深入理解C#:查询集合
- 第16篇 基于Bootstarp 仿京东多条件筛选插件的开发(展示上)
- android开发笔记:listview
- 类指针的定义问题
- python之最简http文件列表显示服务
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- CBitmapButton为按钮加载图片注意事项
- 第17篇 基于Bootstarp 仿京东多条件筛选插件的开发(展示下)
- 队列实现栈
- UI基础__控件布局的几种方法总结
- 一起talk C栗子吧(第六十三回:C语言实例--字符串查找)
- LeetCode---String to Integer (atoi)
- android sqlite cursor 没有返回数据,资源占用问题
- Android之退出应用关闭项目每个Activity的总结