Factory Method

 简单写的一个Factory Method, 利用反射简化工厂逻辑,在添加/删除产品后不需要更新工厂.

 

using  System;
using  System.Collections.Generic;

namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
{
    
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Light l;
            l 
= LightFactory.Create("Bulb");
            l.TurnOn();
            l.TurnOff();

            l 
= LightFactory.Create("Tube");
            l.TurnOn();
            l.TurnOff();           
        }


    }



    
public interface ILightCreator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Light Create();
    }


    
public abstract class Light
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public abstract string Name get; }
        
public abstract void TurnOn();
        
public abstract void TurnOff();
    }


    
class BulbLight : Light, ILightCreator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public override void TurnOn()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"Bulb Light is turned on");
        }


        
public override void TurnOff()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"Bulb Light is turned off");
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
ILightCreator Members#region ILightCreator Members

        
public Light Create()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new BulbLight();
        }


        
#endregion


        
public override string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return "Bulb"; }
        }

    }


    
class TubeLight : Light, ILightCreator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public override void TurnOn()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"Tube Light is turned on");
        }


        
public override void TurnOff()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"Tube light is turned off");
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
ILightCreator Members#region ILightCreator Members

        
public Light Create()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new TubeLight();
        }


        
#endregion


        
public override string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return "Tube"; }
        }

    }


    
public class LightFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public static Light Create(string lightType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (ImplementedLightCreator.ContainsKey(lightType))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ILightCreator creator 
= ImplementedLightCreator[lightType];
                
return creator.Create();
            }

            
else
                
return new BulbLight();
        }


        
static Dictionary<string, ILightCreator> implementedLightCreator;
        
public static Dictionary<string, ILightCreator> ImplementedLightCreator
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (implementedLightCreator == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{                  
                    implementedLightCreator 
= InitilizeCreator();                   
                }

                
return implementedLightCreator;
            }

        }


        
private static Dictionary<string, ILightCreator> InitilizeCreator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Dictionary
<string, ILightCreator> creators = new Dictionary<string, ILightCreator>();

            Type basic 
= typeof(ILightCreator);
            
foreach (Type t in basic.Assembly.GetTypes())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (basic.IsAssignableFrom(t) && !t.IsAbstract)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    ILightCreator instance 
= (ILightCreator)(Activator.CreateInstance(t));
                    creators[(
string)(t.GetProperty("Name").GetValue(instance, null))] = instance;
                }

            }

            
return creators;
        }

    }

}

 

 

 

 

转载于:https://www.cnblogs.com/hiber/archive/2008/07/29/1255496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值