简单写的一个Factory Method, 利用反射简化工厂逻辑,在添加/删除产品后不需要更新工厂.
using
System;
using
System.Collections.Generic;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
ConsoleApplication1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
class Program
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Light l;
l = LightFactory.Create("Bulb");
l.TurnOn();
l.TurnOff();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
l = LightFactory.Create("Tube");
l.TurnOn();
l.TurnOff();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public interface ILightCreator
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Light Create();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public abstract class Light
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public abstract string Name
{ get; }
public abstract void TurnOn();
public abstract void TurnOff();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class BulbLight : Light, ILightCreator
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public override void TurnOn()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Bulb Light is turned on");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void TurnOff()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Bulb Light is turned off");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ILightCreator Members#region ILightCreator Members
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Light Create()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new BulbLight();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return "Bulb"; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class TubeLight : Light, ILightCreator
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public override void TurnOn()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Tube Light is turned on");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void TurnOff()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Tube light is turned off");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ILightCreator Members#region ILightCreator Members
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Light Create()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new TubeLight();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return "Tube"; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class LightFactory
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static Light Create(string lightType)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ImplementedLightCreator.ContainsKey(lightType))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ILightCreator creator = ImplementedLightCreator[lightType];
return creator.Create();
}
else
return new BulbLight();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static Dictionary<string, ILightCreator> implementedLightCreator;
public static Dictionary<string, ILightCreator> ImplementedLightCreator
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (implementedLightCreator == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
implementedLightCreator = InitilizeCreator();
}
return implementedLightCreator;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static Dictionary<string, ILightCreator> InitilizeCreator()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dictionary<string, ILightCreator> creators = new Dictionary<string, ILightCreator>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Type basic = typeof(ILightCreator);
foreach (Type t in basic.Assembly.GetTypes())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (basic.IsAssignableFrom(t) && !t.IsAbstract)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ILightCreator instance = (ILightCreator)(Activator.CreateInstance(t));
creators[(string)(t.GetProperty("Name").GetValue(instance, null))] = instance;
}
}
return creators;
}
}
}