以下是本程序的UML图以及代码:
所需class如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace M_K2 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 KFCSetMeal KFC = new KFCSetMeal(); //client 14 SetMealDirector KFCDirector = new SetMealDirector(KFC); 15 Console.WriteLine("Our team name is KFC..."); 16 KFCDirector.CreateSetMeal(); 17 18 MCDSetMeal MCD = new MCDSetMeal(); 19 SetMealDirector MCDDirector = new SetMealDirector(MCD); 20 Console.WriteLine("Our team name is MCD..."); 21 MCDDirector.CreateSetMeal(); 22 } 23 } 24 } 25 26 abstract class SetMeal //builder 27 { 28 public abstract void AddBurger(); 29 public abstract void AddChips(); 30 public abstract void AddDrink(); 31 } 32 class SetMealDirector //director 33 { 34 private SetMeal _builder; 35 36 public SetMealDirector(SetMeal builder) 37 { 38 _builder = builder; 39 } 40 41 public void CreateSetMeal() 42 { 43 _builder.AddBurger(); 44 _builder.AddChips(); 45 _builder.AddDrink(); 46 } 47 } 48 49 class KFCSetMeal : SetMeal //ConcreteBuilder 50 { 51 public KFCSetMeal() : base(){} 52 53 public override void AddBurger() 54 { 55 Console.WriteLine("I am Burger from KFC..."); 56 } 57 58 public override void AddChips() 59 { 60 Console.WriteLine("I am Chips from KFC..."); 61 } 62 63 public override void AddDrink() 64 { 65 Console.WriteLine("I am Drink from KFC..."); 66 } 67 } 68 69 class MCDSetMeal : SetMeal //ConcreteBuilder 70 { 71 public MCDSetMeal() : base() {} 72 73 public override void AddBurger() 74 { 75 Console.WriteLine("I am Burger from MCD..."); 76 } 77 78 public override void AddChips() 79 { 80 Console.WriteLine("I am Chips from MCD..."); 81 } 82 83 public override void AddDrink() 84 { 85 Console.WriteLine("I am Drink from MCD..."); 86 } 87 }
运行结果如下: