简单工厂模式,抽象工厂模式,反射工厂模式的代码总结

      工厂模式也用了不少,特别是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 " );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值