using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { /// <summary> /// 提供一种封装机制来隔离出构成复杂对象的各个子对象的变化,从而保持系统中的相对稳定的将这些子对象组合在一起的算法不随着需求的改变而改变。 /// 指挥者(Director),建造者(Builder),具体建造者(ConcreteBuilder),产品(Product) /// 产品:需要创建的对象产品 /// 具体建造者:创建产品的实例并且实现建造者多个方法对产品进行装配 /// 建造者:本质为抽象类,里面的抽象方法供具体建造者重写,声明产品的引用 /// 指挥者:调用建造者抽象类以及其方法 /// </summary> class Program { static void Main(string[] args) { //指挥者 CreateDirector _CreateDirector = new CreateDirector(); //建造者 AppleConcreteBuilder _AppleConcreteBuilder = new AppleConcreteBuilder(); _CreateDirector.CreateComputer(_AppleConcreteBuilder); _AppleConcreteBuilder.ComputerInstance.ShowComputerInfo(); Console.ReadLine(); } } class Computer { private string m_CompName; private string m_MemorySize; private string m_DiskSize; /// <summary> /// 电脑名称 /// </summary> public string CompName { get { return m_CompName; } set { m_CompName = value; } } /// <summary> /// 内存大小 /// </summary> public string MemorySize { get { return m_MemorySize; } set { m_MemorySize = value; } } /// <summary> /// 硬盘大小 /// </summary> public string DiskSize { get { return m_DiskSize; } set { m_DiskSize = value; } } /// <summary> /// 显示产品的信息 /// </summary> public void ShowComputerInfo() { Console.WriteLine("------------------------"); Console.WriteLine("电脑名称:" + CompName); Console.WriteLine("内存大小:" + MemorySize); Console.WriteLine("硬盘大小:" + DiskSize); } } //建造者 定义建造产品的抽象方法和产品的引用 abstract class ComputerBuilder { private Computer computerInstance; /// <summary> /// 返回建立的电脑产品实例 /// </summary> public Computer ComputerInstance { get { return computerInstance; } set { computerInstance = value; } } /// <summary> /// 创建电脑名称 /// </summary> abstract public void CreateCompName(); /// <summary> /// 创建电脑内存 /// </summary> abstract public void CreateCompMemory(); /// <summary> /// 创建电脑硬盘 /// </summary> abstract public void CreateCompDisk(); } //具体建造者创建苹果电脑 class AppleConcreteBuilder : ComputerBuilder { public override void CreateCompName() { ComputerInstance = new Computer(); ComputerInstance.CompName = "苹果电脑"; } public override void CreateCompMemory() { ComputerInstance.MemorySize = "4G"; } public override void CreateCompDisk() { ComputerInstance.DiskSize = "500G"; } } //具体建造者创建戴尔电脑 class DellConcreteBuilder : ComputerBuilder { public override void CreateCompName() { ComputerInstance = new Computer(); ComputerInstance.CompName = "戴尔电脑"; } public override void CreateCompMemory() { ComputerInstance.MemorySize = "2GB"; } public override void CreateCompDisk() { ComputerInstance.DiskSize = "320GB"; } } //具体建造者创建惠普电脑 class HpConcreteBuilder : ComputerBuilder { public override void CreateCompName() { ComputerInstance = new Computer(); ComputerInstance.CompName = "惠普电脑"; } public override void CreateCompMemory() { ComputerInstance.MemorySize = "1GB"; } public override void CreateCompDisk() { ComputerInstance.DiskSize = "160GB"; } } //具体建造者创建IBM电脑 class IBMConcreteBuilder : ComputerBuilder { public override void CreateCompName() { ComputerInstance = new Computer(); ComputerInstance.CompName = "IBM电脑"; } public override void CreateCompMemory() { ComputerInstance.MemorySize = "2G"; } public override void CreateCompDisk() { ComputerInstance.DiskSize = "1000G"; } } //指挥者 class CreateDirector { /// <summary> /// 调用建造者抽象方法以生产产品 /// </summary> /// <param name="compBuilder"></param> public void CreateComputer(ComputerBuilder compBuilder) { compBuilder.CreateCompName(); compBuilder.CreateCompMemory(); compBuilder.CreateCompDisk(); } } }