在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、概述 Builder为创建Product对象的各个子对象指定抽象接口。 1 interface ICarBuilder 2 { 3 void BuilderWheels(); 4 void BuilderWindows(); 5 } 接着创建普通汽车的Builder和高级汽车的Builder 1 public class BasicBuilder : ICarBuilder 2 { 3 public void BuilderWheels() 4 { 5 Console.WriteLine("Builder Basic Wheels"); 6 } 7 8 public void BuilderWindows() 9 { 10 Console.WriteLine("Builder Basic Windows"); 11 } 12 } 13 14 public class AdvancedBuilder : ICarBuilder 15 { 16 public void BuilderWheels() 17 { 18 Console.WriteLine("Builder Advanced Wheels"); 19 } 20 21 public void BuilderWindows() 22 { 23 Console.WriteLine("Builder Advanced Windows"); 24 } 25 } 然后创建汽车建造工厂,它使用Builder来创建不同种类的汽车 1 class CarFactory 2 { 3 public ICarBuilder Builder { get; set; } 4 5 public CarFactory(ICarBuilder builder) 6 { 7 Builder = builder; 8 } 9 10 public void CreateCar() 11 { 12 Builder.BuilderWheels(); 13 Builder.BuilderWindows(); 14 } 15 } 最后看一下如何调用 1 static void Main(string[] args) 2 { 3 CarFactory game = new CarFactory(new BasicBuilder()); 4 game.CreateCar(); 5 game.Builder = new AdvancedBuilder(); 6 game.CreateCar(); 7 Console.ReadLine(); 8 } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论