C# 建造者模式(Builder Pattern)
创始人
2025-01-15 06:38:02
0

建造者模式(Builder Pattern),也被称为生成器模式,是一种对象构建模式,旨在将复杂对象的构建过程与表示分离,使得同样的构建过程可以创建不同的表示。这种模式特别适用于构建具有多个组成部分的复杂对象,且这些组成部分的创建和组装方式相对独立。

public class BuilderPatternTest {     public void Main()     {         var director = new Director();//指挥者1         var director1 = new Director1();//指挥者2         var desktopBuilder = new DesktopBuilder();//建造者1         var desktopBuilder2 = new DesktopBuilder2();//建造者2          //第一台电脑,使用指挥者1,使用建造者1         var computer1 = director.Construct(desktopBuilder);         Console.WriteLine(computer1.ToString());          //第二台电脑,使用指挥者2,使用建造者2         var computer2 = director1.Construct(desktopBuilder);         Console.WriteLine(computer2.ToString());          //第三台电脑,使用指挥者1,使用建造者2         var computer3 = director.Construct(desktopBuilder2);         Console.WriteLine(computer3.ToString());          //第四台电脑,使用建造者1,不使用指挥者         var computer4 = new DesktopBuilder()             .SetType("平板电脑")             .SetRam(256)             .SetProcessor("i13212")             .Build();         Console.WriteLine(computer4.ToString());     } }  // 定义产品类 public class Computer {     public string Type { get; }     public bool IsGraphicsCardIncluded { get; }     public int Ram { get; }     public string Processor { get; }      public Computer(string type, bool isGraphicsCardIncluded, int ram, string processor)     {         Type = type;         IsGraphicsCardIncluded = isGraphicsCardIncluded;         Ram = ram;         Processor = processor;     }      public override string ToString()     {         return $"Type: {Type}, Graphics Card: {IsGraphicsCardIncluded}, RAM: {Ram}GB, Processor: {Processor}";     } }  // 抽象建造者 // 也可以使用接口实现抽象建造者 // 方法中返回自身,以便链式调用 public abstract class Builder {     protected string Type;     protected bool IsGraphicsCardIncluded;     protected int Ram;     protected string Processor;      public abstract Computer Build();      public virtual Builder SetType(string type)     {         return this;     }      public virtual Builder SetGraphicsCard(bool isGraphicsCardIncluded)     {         return this;     }      public virtual Builder SetRam(int ram)     {         return this;     }      public virtual Builder SetProcessor(string processor)     {         return this;     } }  // 具体建造者 public class DesktopBuilder : Builder {     public override Computer Build()     {         return new Computer(Type, IsGraphicsCardIncluded, Ram, Processor);     }      public override DesktopBuilder SetType(string type)     {         Type = type;         return this;     }      public override DesktopBuilder SetGraphicsCard(bool isGraphicsCardIncluded)     {         IsGraphicsCardIncluded = isGraphicsCardIncluded;         return this;     }      public override DesktopBuilder SetRam(int ram)     {         Ram = ram;         return this;     }      public override DesktopBuilder SetProcessor(string processor)     {         Processor = processor;         return this;     } }  public class DesktopBuilder2 : Builder {     public override Computer Build()     {         return new Computer(Type, IsGraphicsCardIncluded, Ram, Processor);     }      public override DesktopBuilder2 SetType(string type)     {         Type = type;         return this;     }      public override DesktopBuilder2 SetGraphicsCard(bool isGraphicsCardIncluded)     {         IsGraphicsCardIncluded = isGraphicsCardIncluded;         return this;     }      public override DesktopBuilder2 SetRam(int ram)     {         Ram = ram;         return this;     } }  // 导演类 public class Director {     public Computer Construct(Builder builder)     {         builder.SetType("台式电脑");         builder.SetGraphicsCard(true);         builder.SetRam(16);         builder.SetProcessor("i7");         return builder.Build();     } }  public class Director1 {     public Computer Construct(Builder builder)     {         builder.SetType("笔记本电脑");         builder.SetGraphicsCard(true);         builder.SetRam(128);         builder.SetProcessor("i1000");         return builder.Build();     } }

相关内容

热门资讯

此事引发网友热议!吉祥填大坑脚... 此事引发网友热议!吉祥填大坑脚本,打罗松必胜技巧,方针教程(今日头条)-哔哩哔哩1、玩家可以在吉祥填...
玩家必用!hhpoker作必弊... 玩家必用!hhpoker作必弊实战视频,新星游房间链接辅助,总是有辅助透视(讲解有挂)1、进入到hh...
据目击者称!wepoker私人... 据目击者称!wepoker私人局怎么玩(透视)确实是有辅助神器(有挂规律)-哔哩哔哩1、wepoke...
解密透视!xpoker辅助助手... 解密透视!xpoker辅助助手!实测揭晓辅助器(有挂讲解)-哔哩哔哩;1、进入到xpoker辅助助手...
目前!潮汕馆破解版,福建大玩家... 目前!潮汕馆破解版,福建大玩家辅助是真的吗,绝活教程(有挂头条)-哔哩哔哩1、在福建大玩家辅助是真的...
揭秘攻略!wepoker透视有... 揭秘攻略!wepoker透视有用吗,闲逸平台辅助功能入口在哪里,本来是有辅助安装(有挂秘笈)1、许多...
反观!wepoker破解器(透... 反观!wepoker破解器(透视)真是有辅助app(有挂工具)-哔哩哔哩1、下载好wepoker破解...
科普透视!德普之星辅助器app... 科普透视!德普之星辅助器app!今日百科辅助插件(存在有挂)-哔哩哔哩1、实时德普之星辅助器app透...
近年来!天天贵州万能辅助器,摸... 近年来!天天贵州万能辅助器,摸一把跑得快辅助器,策略教程(有挂秘籍)-哔哩哔哩亲,关键说明,天天贵州...
关于!wepoker免费脚本,... 关于!wepoker免费脚本,潮友会虾蟹骰子概率计算方式,真是是真的有辅助教程(有挂辅助)该软件可以...