建造者模式

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace KFC_MacDonald_Bulid
  8 {
  9     class Program
 10     {
 11         //客户端代码
 12         static void Main(string[] args)
 13         {
 14 
 15             KFCBuilder kfc = new KFCBuilder();
 16             MealDirector kfcdirector = new MealDirector(kfc);
 17             kfcdirector.CreateMeal().Show();
 18 
 19             MBuilder m = new MBuilder();
 20             MealDirector mdirector = new MealDirector(m);
 21             mdirector.CreateMeal().Show();
 22 
 23         }
 24 
 25         //Meal类
 26         class Meal
 27         {
 28             protected List<Part> _parts = new List<Part>();
 29             public void AddPart(Part part)
 30             {
 31                 _parts.Add(part);
 32             }
 33 
 34             public void Show()
 35             {
 36                 foreach(Part part in _parts)
 37                 {
 38                     part.Show();
 39                 }
 40             }
 41         }
 42 
 43         //Part类
 44         abstract class Part
 45         {
 46             public Part()
 47             {}
 48 
 49             public abstract void Show();
 50         }
 51 
 52         class KFCBurger:Part
 53         {
 54 
 55             public KFCBurger():base()
 56             { }
 57             public override void Show()
 58             {
 59                 Console.WriteLine("KFCBurger is ready");//输出"KFCBurger is ready"    
 60             }
 61         }
 62 
 63         class KFCChips : Part
 64         {
 65              public KFCChips():base()
 66             { }
 67             public override void Show()
 68             {
 69                 Console.WriteLine("KFCChips is ready");//输出"KFCChips is ready"    
 70             }
 71         }
 72 
 73         class KFCDrink : Part
 74         {
 75             public KFCDrink()
 76                 : base()
 77             { }
 78             public override void Show()
 79             {
 80                 Console.WriteLine("KFCDrink is ready");//输出"KFCDrink is ready"    
 81             }
 82         }
 83 
 84         class MBurger:Part
 85         {
 86              public MBurger():base()
 87             { }
 88             public override void Show()
 89             {
 90                 Console.WriteLine("MBurger is ready");//输出"KFCBurger is ready"    
 91             }
 92         }
 93 
 94         class MChips : Part
 95         {
 96             public MChips():base()
 97             { }
 98             public override void Show()
 99             {
100                 Console.WriteLine("MChips is ready");//输出"MChips is ready"    
101             }
102         }
103 
104         class MDrink : Part
105         {
106                public MDrink():base()
107             { }
108             public override void Show()
109             {
110                 Console.WriteLine("MDrink is ready");//输出"MDrink is ready"    
111             }
112         }
113         //抽象建造者
114         abstract class MealBuilder
115         {
116             protected string _g;
117             protected Meal _meal = new Meal();
118             public MealBuilder()
119             {  }
120             public abstract void BuildBurger();
121             public abstract void BuildChips();
122             public abstract void BuildDrink();
123             public Meal GetMeal()
124             {
125                 return _meal;
126             }
127         }
128 
129         //具体建造者
130         class KFCBuilder:MealBuilder
131         {
132              public KFCBuilder():base()
133             { }
134 
135             public override void BuildBurger()
136             {
137                 _meal.AddPart(new KFCBurger());
138             }
139 
140             public override void BuildChips()
141             {
142                 _meal.AddPart(new KFCChips());
143             }
144 
145             public override void BuildDrink()
146             {
147                 _meal.AddPart(new KFCDrink());
148             }
149         }
150 
151         class MBuilder : MealBuilder
152         {
153               public MBuilder(): base()
154               {}
155             
156 
157             public override void BuildBurger()
158             {
159                 _meal.AddPart(new MBurger());
160             }
161 
162             public override void BuildChips()
163             {
164                 _meal.AddPart(new MChips());
165             }
166 
167             public override void BuildDrink()
168             {
169                 _meal.AddPart(new MDrink());
170             }
171         }
172         //指挥者
173         class MealDirector
174         {
175             private MealBuilder _builder;
176             public MealDirector(MealBuilder builder)
177             {
178                 _builder = builder;
179             }
180 
181             public Meal CreateMeal()
182             {
183                 _builder.BuildBurger();
184                 _builder.BuildChips();
185                 _builder.BuildDrink();
186 
187                 return _builder.GetMeal();
188             }
189         }
190 
191     }
192 }

 

posted @ 2015-12-06 17:09  Auth  阅读(239)  评论(0编辑  收藏  举报