// See https://aka.ms/new-console-template for more information /* 个人理解 获取要组装的东西 叫人去组装 获取组装好的东西展示 */ Console.WriteLine("Hello, World!"); XiaoMi xiaoMi = new XiaoMi();// Builder builder1 = new ConcreteBuilder1(); xiaoMi.ConstructPhone(builder1); Phone phone = builder1.GetBuilder(); phone.Show(); public class XiaoMi { public void ConstructPhone(Builder builder) { builder.BuildPartCamera(); builder.BuildPartScreen(); } } public class Phone { private List<string> list = new(); public void Add(string part) { list.Add(part); } public void Show() { Console.WriteLine("手机开始在组装......."); foreach (string part in list) { Console.WriteLine("组件" + part + "已装好"); } Console.WriteLine("手机组装好了"); } } public abstract class Builder { /// <summary> /// 摄像头 /// </summary> public abstract void BuildPartCamera(); /// <summary> /// 屏幕 /// </summary> public abstract void BuildPartScreen(); public abstract Phone GetBuilder(); } public class ConcreteBuilder1 : Builder { Phone phone = new Phone(); public override void BuildPartCamera() { phone.Add("装相机"); } public override void BuildPartScreen() { phone.Add("装屏幕"); } public override Phone GetBuilder() { return phone; } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288348.html