学习TerryLee的设计模式颇有感触,留下以下笔记以作日后参考。
代码
//-----------------------------------------------
//创建者模式是处理一系列组合对象的创建,且这种组合方式是不同的。
//-----------------------------------------------
#region 产品
public class Product
{
public List<string> SomeThing = new List<string>();
}
#endregion
#region 产品的构造器
public abstract class AbstractBuilder
{
public abstract void BildA();
public abstract void BildB();
public abstract void BildC();
public abstract void BildD();
public abstract Product BuilProduct();
}
public class ABuilder : AbstractBuilder
{
private Product product = new Product();
public override void BildA()
{
product.SomeThing.Add("Aa");
}
public override void BildB()
{
product.SomeThing.Add("Ab");
}
public override void BildC()
{
product.SomeThing.Add("Ac");
}
public override void BildD()
{
product.SomeThing.Add("Ad");
}
public override Product BuilProduct()
{
return product;
}
}
public class BBuilder : AbstractBuilder
{
private Product product = new Product();
public override void BildA()
{
product.SomeThing.Add("Ba");
}
public override void BildB()
{
product.SomeThing.Add("Bb");
}
public override void BildC()
{
product.SomeThing.Add("Bc");
}
public override void BildD()
{
product.SomeThing.Add("Bd");
}
public override Product BuilProduct()
{
return product;
}
}
#endregion
#region 工厂
public class ProductFactory
{
public Product CreateProduct(AbstractBuilder builder)
{
return builder.BuilProduct();
}
}
#endregion
#region 客户端
public class App
{
public static void Main(string[] args)
{
ProductFactory factory = new ProductFactory();
string strbuiderName = "ABuilder";
AbstractBuilder buider;
buider = (AbstractBuilder)Assembly.Load("FactoryMethod").CreateInstance("FactoryMethod." + strbuiderName);
Product product = factory.CreateProduct(buider);
}
}
#endregion