建造者模式是一种创建型设计模式,旨在让你能够创建复杂对象,而无需直接在应用程序中指定其具体类型。通过建造者模式,你可以将对象的构建过程和表示分离,从而使相同的构建过程可以创建不同表示形式的对象。这种模式通常包括以下几个关键组件:
-
产品(Product):最终要创建的复杂对象。
-
抽象建造者(Builder):声明了用于创建产品各个部件的方法接口。
-
具体建造者(Concrete Builder):实现了抽象建造者接口,负责实际构建产品的各个部件。
-
指挥者(Director):构建一个使用Builder接口的对象,负责按照一定顺序构建产品。
在使用建造者模式时,通常创建一个指挥者对象,然后将具体建造者对象传给指挥者,指挥者按照特定顺序调用建造者的方法来构建产品。这种模式的优势在于能够方便地构建复杂对象,同时将构建过程与表示分离,提高了灵活性和可维护性。
Product:
/// <summary> /// 手机接口 /// </summary> public interface IPhone { /// <summary> /// 名称 /// </summary> string Name { get; set; } /// <summary> /// CPU /// </summary> string CPU { get; set; } /// <summary> /// 屏幕 /// </summary> string Screen { get; set; } /// <summary> /// 内存 /// </summary> string Memory { get; set; } /// <summary> /// 相机 /// </summary> string Camera { get; set; } /// <summary> /// 电池 /// </summary> string Battery { get; set; } }
苹果手机
public class ApplePhone : IPhone { /// <summary> /// 手机名称 /// </summary> public string Name { get; set; } = "IPhone"; /// <summary> /// CPU /// </summary> public string CPU { get; set; } /// <summary> /// 手机屏幕 /// </summary> public string Screen { get; set; } /// <summary> /// 手机内存 /// </summary> public string Memory { get; set; } /// <summary> /// 手机摄像头 /// </summary> public string Camera { get; set; } /// <summary> /// 手机电池 /// </summary> public string Battery { get; set; } }
三星手机
public class SamsungPhone : IPhone { /// <summary> /// 手机名称,默认三星手机 /// </summary> public string Name { get; set; } = "SamsungPhone"; /// <summary> /// CPU /// </summary> public string CPU { get; set; } /// <summary> /// 手机屏幕 /// </summary> public string Screen { get; set; } /// <summary> /// 手机内存 /// </summary> public string Memory { get; set; } /// <summary> /// 手机摄像头 /// </summary> public string Camera { get; set; } /// <summary> /// 手机电池 /// </summary> public string Battery { get; set; } }
Builder
抽象建造者可以用接口和抽象类。
接口方式:
public interface IBuilder { /// <summary> /// 手机 /// </summary> IPhone Phone { get; set; } /// <summary> /// CPU /// </summary> void BuildCPU(); /// <summary> /// 屏幕 /// </summary> void BuildScreen(); /// <summary> /// 内存 /// </summary> void BuildMemory(); /// <summary> /// 摄像头 /// </summary> void BuildCamera(); /// <summary> /// 电池 /// </summary> void BuildBattery(); /// <summary> /// 建构方法 /// </summary> /// <returns></returns> IPhone GetPhone(); }
抽象类方式:
public abstract class Builder { /// <summary> /// 手机 /// </summary> protected IPhone Phone { get; set; } /// <summary> /// CPU /// </summary> public abstract void BuildCPU(); /// <summary> /// 屏幕 /// </summary> public abstract void BuildScreen(); /// <summary> /// 内存 /// </summary> public abstract void BuildMemory(); /// <summary> /// 摄像头 /// </summary> public abstract void BuildCamera(); /// <summary> /// 电池 /// </summary> public abstract void BuildBattery(); /// <summary> /// 建构方法 /// </summary> /// <returns></returns> public IPhone GetPhone() { return Phone; } }
Concrete Builder
建构苹果手机第一代
public class Iphone1Builder : IBuilder { /// <summary> /// 苹果手机 /// </summary> public IPhone Phone { get; set; } = new ApplePhone(); public void BuildBattery() { Phone.Battery = "电池 内置1030毫安锂电池 "; } public void BuildCamera() { Phone.Camera = "摄像头 后置200万像素,无前置摄像头"; } public void BuildCPU() { Phone.CPU = "芯片 三星S5L8900 "; } public void BuildMemory() { Phone.Memory = "存储 128MB+4G/8G/16G "; } public void BuildScreen() { Phone.Screen = "屏幕 3.5英寸多点触控显示屏"; } public IPhone GetPhone() { return Phone; } }
建构苹果手机第15代
public class Iphone15Builder : IBuilder { public IPhone Phone { get; set; } = new ApplePhone(); public void BuildBattery() { Phone.Battery = "电池 3349mAh锂电池 "; } public void BuildCamera() { Phone.Camera = "摄像头 后置4800W像素,前置1200W摄像头"; } public void BuildCPU() { Phone.CPU = "芯片 A16仿生芯片,16核神经引擎,5核GPU、6核CPU "; } public void BuildMemory() { Phone.Memory = "存储 2G+128G/256G/512G "; } public void BuildScreen() { Phone.Screen = "屏幕 6.1英寸多点触控显示屏 "; } public IPhone GetPhone() { return Phone; } }
建构三星手机
public class GalaxyS24Builder : Builder { public GalaxyS24Builder() { base.Phone = new SamsungPhone(); } public override void BuildBattery() { Phone.Battery = "电池:6000mAh"; } public override void BuildCamera() { Phone.Camera = "摄像头:后置10000W像素"; } public override void BuildCPU() { Phone.CPU = "芯片:66核CPU"; } public override void BuildMemory() { Phone.Memory= "存储:16G+1024G"; } public override void BuildScreen() { Phone.Screen= "屏幕:8.5英寸"; } }
Director
/// <summary> /// 指挥官 或 导演 ,作用是指挥 Builder 去建构具体的手机 /// </summary> public class Director { private Builder Builder; private IBuilder builder; public Director(IBuilder builder) { this.builder = builder; } public Director(Builder builder) { this.Builder = builder; } public IPhone DoWork(IBuilder _builder = null) { if (_builder != null) { this.builder = _builder; } builder.BuildBattery(); builder.BuildCamera(); builder.BuildCPU(); builder.BuildMemory(); builder.BuildScreen(); return builder.GetPhone(); } public IPhone DoBuild(Builder _builder = null) { if (_builder != null) { this.Builder = _builder; } Builder.BuildBattery(); Builder.BuildCamera(); Builder.BuildCPU(); Builder.BuildMemory(); Builder.BuildScreen(); return Builder.GetPhone(); } }
调用
public class Client { public void Start() { Console.WriteLine($"2007年开始生产第一台苹果手机"); IBuilder builder = new Iphone1Builder();//builder负责组装iphone1手机 Director director = new Director(builder);//director指挥builder干活儿 IPhone phone = director.DoWork(); DoStuff(phone); Console.WriteLine("2023年苹果发布了IPhone15"); director.DoWork(new Iphone15Builder()); DoStuff(director.DoWork()); Console.WriteLine("同年三星发布了GalaxyS24手机"); director= new Director(new GalaxyS24Builder()); DoStuff(director.DoBuild()); } private void DoStuff(IPhone phone) { Console.WriteLine(phone.Name); Console.WriteLine(phone.CPU); Console.WriteLine(phone.Screen); Console.WriteLine(phone.Memory); Console.WriteLine(phone.Battery); Console.WriteLine(phone.Camera); } }
static void Main(string[] args) { new Client().Start(); Console.ReadKey(); }
标签:builder,set,05,void,get,建造,模式,Phone,public From: https://www.cnblogs.com/MingQiu/p/18062327