Factory Method

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

 

using System;
using System.Collections.Generic;

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

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


    }



    
public interface ILightCreator
    
{
        Light Create();
    }


    
public abstract class Light
    
{
        
public abstract string Name get; }
        
public abstract void TurnOn();
        
public abstract void TurnOff();
    }


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


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


        
ILightCreator Members

        
public override string Name
        
{
            
get return "Bulb"; }
        }

    }


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


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


        
ILightCreator Members

        
public override string Name
        
{
            
get return "Tube"; }
        }

    }


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

            
else
                
return new BulbLight();
        }


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

                
return implementedLightCreator;
            }

        }


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

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

            }

            
return creators;
        }

    }

}

 

 

 

 

posted @ 2008-07-29 13:21  芭蕉  阅读(235)  评论(0编辑  收藏