学习TerryLee的设计模式颇有感触,留下以下笔记以作日后参考。
代码
//-----------------------------------------------
//抽象工厂是处理复合对象的创建。
//和工厂方法相同将类的创建推向了客户端更把创建的逻辑推向了客户端。
//-----------------------------------------------
#region 产品
#region 简单产品
public abstract class Monitor
{
}
public abstract class Box
{
}
#endregion
#region 复合产品
public abstract class PC
{
public abstract Monitor CreateMonitor();
public abstract Box CreateBox();
}
public class TypeAPC : PC
{
public override Monitor CreateMonitor()
{
throw new NotImplementedException();
}
public override Box CreateBox()
{
throw new NotImplementedException();
}
}
public class TypeBPC : PC
{
public override Monitor CreateMonitor()
{
throw new NotImplementedException();
}
public override Box CreateBox()
{
throw new NotImplementedException();
}
}
#endregion
#endregion
#region 工厂
public class PCFactory
{
public Monitor pen;
public Box Box;
public void CreatPC(PC some)
{
pen = some.CreateMonitor();
Box = some.CreateBox();
}
}
#endregion
#region 客户端
public class App
{
public static void Main(string[] args)
{
string strfactoryName = "TypeAPC";
PC pc;
pc = (PC)Assembly.Load("FactoryMethod").CreateInstance("FactoryMethod." + strfactoryName);
PCFactory factory = new PCFactory();
factory.CreatPC(pc);
}
}
#endregion