学习TerryLee的设计模式颇有感触,留下以下笔记以作日后参考。
代码
//-----------------------------------------------
//工厂方法是简单工厂的扩展。
//工厂方法不只是将类的创建推向了客户端更把创建的逻辑推向了客户端。
//-----------------------------------------------
#region 产品
public interface ICoat
{ }
public class ACoat : ICoat
{ }
public class BCoat : ICoat
{ }
public class CCoat : ICoat
{ }
#endregion
#region 工厂
public interface IFactory
{
ICoat CreateCoat();
}
public class AFactory : IFactory
{
#region IFactory Members
public ICoat CreateCoat()
{
throw new NotImplementedException();
}
#endregion
}
public class BFactory : IFactory
{
#region IFactory Members
public ICoat CreateCoat()
{
throw new NotImplementedException();
}
#endregion
}
public class CFactory : IFactory
{
#region IFactory Members
public ICoat CreateCoat()
{
throw new NotImplementedException();
}
#endregion
}
#endregion
#region 客户端
public class App
{
public static void Main(string[] args)
{
string strfactoryName = "AFactory";
IFactory factory;
factory = (IFactory)Assembly.Load("FactoryMethod").CreateInstance("FactoryMethod." + strfactoryName);
ICoat log = factory.CreateCoat();
}
}
#endregion