代码及解释如下:
public static void BuilderPattern()
{
//我要买一个dell电脑
ComputerVender v = new ComputerVender();
Computer computerObj= v.GetComputer(ComputerBrand.DELL);
Console.WriteLine(computerObj.ToString());
//如果另外一个人想买联想电脑,只需告诉ComputerVender 品牌信息即可,其它地方无需修改
//由于每个产品的构造都一样,不同的只是各个部分的具体型号,所以构造产品的过程是一致的,
//而每个产品零部件的构造则是不相同的(比如我要AMD的CPU,你要Intel的CPU),这正是建造者模式
//的用武之地:为外界提供一致的产品构造过程,封装内部零部件的构造细节(外界不用关心)。
//其它应用场景:1,服装搭配.2,话费套餐 3,游戏角色等
}
/// <summary>
/// 产品
/// </summary>
public class Computer
{
public string Brand { get; set; }
public string CPU { get; set; }
public string Memory { get; set; }
public string Disk { get; set; }
public string Monitor { get; set; }
public override string ToString()
{
return string.Format("品牌:{0},CPU:{1}, 内存:{2},硬盘:{3},显示器:{4}",
Brand, CPU, Memory,Monitor);
}
}
/// <summary>
/// 建造者基类
/// </summary>
public abstract class ComputerBuilder
{
private Computer mComputer;
public Computer Product
{
get
{
return mComputer;
}
}
public ComputerBuilder(string brand)
{
mComputer = new Computer() { Brand=brand};
}
public abstract void AddCPU();
public abstract void AddMemory( );
public abstract void AddDisk( );
public abstract void AddMonitor( );
}
/// <summary>
/// 建造者具体类
/// </summary>
public class DellComputerBuilder : ComputerBuilder
{
public DellComputerBuilder()
: base("DELL")
{
}
public override void AddCPU( )
{
Product.CPU = "AMD 2100";
}
public override void AddMemory( )
{
Product.Memory = "Kinston DDR3";
}
public override void AddDisk( )
{
Product.Memory = "希捷500G";
}
public override void AddMonitor( )
{
Product.Monitor = "三星显示器";
}
}
public class LenovoComputerBuilder : ComputerBuilder
{
public LenovoComputerBuilder() : base("Lenovo") {
}
public override void AddCPU( )
{
Product.CPU = "Intel i5";
}
public override void AddMemory( )
{
Product.Memory = "Kinston DDR3 4G";
}
public override void AddDisk( )
{
Product.Disk = "三星7500转 500G";
}
public override void AddMonitor( )
{
Product.Monitor = "LG 22英才LED显示器";
}
}
public enum ComputerBrand
{
DELL,
LENOVO
}
/// <summary>
/// 电脑零售商--Director角色
/// </summary>
public class ComputerVender
{
private ComputerBuilder mBuilder;
public Computer GetComputer(ComputerBrand brand)
{
switch (brand)
{
case ComputerBrand.DELL:
mBuilder = new DellComputerBuilder();
break;
case ComputerBrand.LENOVO:
mBuilder = new LenovoComputerBuilder();
break;
default:
throw new ArgumentException("没有此品牌的电脑");
}
mBuilder.AddCPU();
mBuilder.AddMemory();
mBuilder.AddDisk();
mBuilder.AddMonitor();
return mBuilder.Product;
}
{
//我要买一个dell电脑
ComputerVender v = new ComputerVender();
Computer computerObj= v.GetComputer(ComputerBrand.DELL);
Console.WriteLine(computerObj.ToString());
//如果另外一个人想买联想电脑,只需告诉ComputerVender 品牌信息即可,其它地方无需修改
//由于每个产品的构造都一样,不同的只是各个部分的具体型号,所以构造产品的过程是一致的,
//而每个产品零部件的构造则是不相同的(比如我要AMD的CPU,你要Intel的CPU),这正是建造者模式
//的用武之地:为外界提供一致的产品构造过程,封装内部零部件的构造细节(外界不用关心)。
//其它应用场景:1,服装搭配.2,话费套餐 3,游戏角色等
}
/// <summary>
/// 产品
/// </summary>
public class Computer
{
public string Brand { get; set; }
public string CPU { get; set; }
public string Memory { get; set; }
public string Disk { get; set; }
public string Monitor { get; set; }
public override string ToString()
{
return string.Format("品牌:{0},CPU:{1}, 内存:{2},硬盘:{3},显示器:{4}",
Brand, CPU, Memory,Monitor);
}
}
/// <summary>
/// 建造者基类
/// </summary>
public abstract class ComputerBuilder
{
private Computer mComputer;
public Computer Product
{
get
{
return mComputer;
}
}
public ComputerBuilder(string brand)
{
mComputer = new Computer() { Brand=brand};
}
public abstract void AddCPU();
public abstract void AddMemory( );
public abstract void AddDisk( );
public abstract void AddMonitor( );
}
/// <summary>
/// 建造者具体类
/// </summary>
public class DellComputerBuilder : ComputerBuilder
{
public DellComputerBuilder()
: base("DELL")
{
}
public override void AddCPU( )
{
Product.CPU = "AMD 2100";
}
public override void AddMemory( )
{
Product.Memory = "Kinston DDR3";
}
public override void AddDisk( )
{
Product.Memory = "希捷500G";
}
public override void AddMonitor( )
{
Product.Monitor = "三星显示器";
}
}
public class LenovoComputerBuilder : ComputerBuilder
{
public LenovoComputerBuilder() : base("Lenovo") {
}
public override void AddCPU( )
{
Product.CPU = "Intel i5";
}
public override void AddMemory( )
{
Product.Memory = "Kinston DDR3 4G";
}
public override void AddDisk( )
{
Product.Disk = "三星7500转 500G";
}
public override void AddMonitor( )
{
Product.Monitor = "LG 22英才LED显示器";
}
}
public enum ComputerBrand
{
DELL,
LENOVO
}
/// <summary>
/// 电脑零售商--Director角色
/// </summary>
public class ComputerVender
{
private ComputerBuilder mBuilder;
public Computer GetComputer(ComputerBrand brand)
{
switch (brand)
{
case ComputerBrand.DELL:
mBuilder = new DellComputerBuilder();
break;
case ComputerBrand.LENOVO:
mBuilder = new LenovoComputerBuilder();
break;
default:
throw new ArgumentException("没有此品牌的电脑");
}
mBuilder.AddCPU();
mBuilder.AddMemory();
mBuilder.AddDisk();
mBuilder.AddMonitor();
return mBuilder.Product;
}
}