Design Pattern - Builder
一个制造个人电脑的通用的构造器:
public class Builder
{
public virtual String BuildCPU()
{
return "Standard CPU";
}
public virtual String BuildDisplay()
{
return "Standard Display";
}
public virtual String BuildKeyboard()
{
return "Standard Keyboard";
}
public virtual String BuildCDRom()
{
return "Standard CDRom";
}
}
{
public virtual String BuildCPU()
{
return "Standard CPU";
}
public virtual String BuildDisplay()
{
return "Standard Display";
}
public virtual String BuildKeyboard()
{
return "Standard Keyboard";
}
public virtual String BuildCDRom()
{
return "Standard CDRom";
}
}
dell / inter 各自的个人电脑配置器:
public class DellBuilder : Builder
{
public override String BuildDisplay()
{
return "Dell Display";
}
public override String BuildKeyboard()
{
return "Dell Keyboard";
}
}
{
public override String BuildDisplay()
{
return "Dell Display";
}
public override String BuildKeyboard()
{
return "Dell Keyboard";
}
}
public class InterBuilder : Builder
{
public override String BuildCPU()
{
return "Inter P4 CPU";
}
}
配置出来的个人电脑(主机部分)产品:{
public override String BuildCPU()
{
return "Inter P4 CPU";
}
}
public class PCProduct
{
private String cpu, cdrom, keyboard;
public PCProduct(String c, String d, String k)
{
this.cpu = c;
this.cdrom = d;
this.keyboard = k;
}
public void Show()
{
Console.WriteLine("{0}, {1}, {2}", cpu, cdrom, keyboard);
}
}
{
private String cpu, cdrom, keyboard;
public PCProduct(String c, String d, String k)
{
this.cpu = c;
this.cdrom = d;
this.keyboard = k;
}
public void Show()
{
Console.WriteLine("{0}, {1}, {2}", cpu, cdrom, keyboard);
}
}
产生电脑主机产品的指导者:
public class Director
{
public PCProduct Construct(Builder b)
{
return new PCProduct(b.BuildCPU(), b.BuildDisplay(), b.BuildKeyboard());
}
}
测试程序:{
public PCProduct Construct(Builder b)
{
return new PCProduct(b.BuildCPU(), b.BuildDisplay(), b.BuildKeyboard());
}
}
public static void Main()
{
Builder b = new DellBuilder();
Director d = new Director();
PCProduct p = d.Construct(b);
p.Show();
b = new InterBuilder();
p= d.Construct(b);
p.Show();
Console.ReadLine();
}
{
Builder b = new DellBuilder();
Director d = new Director();
PCProduct p = d.Construct(b);
p.Show();
b = new InterBuilder();
p= d.Construct(b);
p.Show();
Console.ReadLine();
}
Builder Demo