设计模式之美:Builder(生成器)(二)
oductBuilder builder)
69 {
70 builder.BuildValueDependOnWeatherPart(@"PM2.5 < 50");
71 builder.BuildValueDependOnFortunePart(@"Good Luck");
72 }
73
74 public void ConstructWithBadWeatherAndBadLuck(AbstractComplexProductBuilder builder)
75 {
76 builder.BuildValueDependOnWeatherPart(@"PM2.5 > 500");
77 builder.BuildValueDependOnFortunePart(@"Bad Luck");
78 }
79 }
80
81 public class Client
82 {
83 public void TestCase1()
84 {
85 AbstractComplexProductBuilder builder = new ConcreteProductBuilderA("Sunday", 9);
86 GoodWeatherAndGoodLuckDirector director = new GoodWeatherAndGoodLuckDirector();
87
88 builder.BeginBuild();
89 director.ConstructWithGoodWeatherAndGoodLuck(builder);
90 ComplexProduct productWithGoodLuck = builder.EndBuild();
91
92 builder.BeginBuild();
93 director.ConstructWithBadWeatherAndBadLuck(builder);
94 ComplexProduct productWithBadLuck = builder.EndBuild();
95 }
96 }
97 }
复制代码
实现方式(二):Builder 将构件返回给 Director,Director 将构件传递给 Builder 中的下一个步骤。
Builder 逐步的构造产品,所以其接口必须足够的普遍。如果构造过程中需要访问前面已经构造了的产品构件,则 Builder 将构件返回给 Director,由 Director 将构件传递给 Builder 中的下一个步骤。
复制代码
1 namespace BuilderPattern.Implementation2
2 {
3 public class ComplexProduct
4 {
5 public string ValueDependOnWeather { get; set; }
6 public string ValueDependOnFortune { get; set; }
7 }
8
9 public abstract class AbstractComplexProductBuilder
10 {
11 protected ComplexProduct _complexProduct;
12
13 public void BeginBuild(ComplexProduct existingComplexProduct = null)
14 {
15 if (existingComplexProduct == null)
16 _complexProduct = new ComplexProduct();
17 else
18 _complexProduct = existingComplexProduct;
19 }
20
21 public virtual string BuildValueDependOnWeatherPart(string weather)
22 {
23 // could do nothing by default
24 _complexProduct.ValueDependOnWeather = weather;
25 return _complexProduct.ValueDependOnWeather;
26 }
27
28 public virtual string BuildValueDependOnFortunePart(string luck, string combinedWithWeather)
29 {
30 // could do nothing by default
31 _complexProduct.ValueDependOnFortune = luck + combinedWithWeather;
32 return _complexProduct.ValueDependOnFortune;
33 }
34
35 public ComplexProduct EndBuild()
36 {
37 return this._complexProduct;
38 }
39 }
40
41 public class ConcreteProductBuilderA : AbstractComplexProductBuilder
42 {
43 private string _dayOfWeek;
44 private int _luckyNumber;
45
46 public ConcreteProductBuilderA(string dayOfWeek, int luckyNumber)
47 {
48 _dayOfWeek = dayOfWeek;
49 _luckyNumber = luckyNumber;
50 }
51
52 public override string BuildValueDependOnWeatherPart(string weather)
53 {
54 // something customized
55 _complexProduct.ValueDependOnWeather = _dayOfWeek + " is " + weather;
56 return _complexProduct.ValueDependOnWeather;
57 }
58
59 public override string BuildValueDependOnFortunePart(string luck, string combinedWithWeather)
60 {
61 // something customized
62 if (_luckyNumber == 8)
63 _complexProduct.ValueDependOnFortune = "Supper" + luck + combinedWithWeather;
64 else
65 _complexProduct.ValueDependOnFortune = "Just so so" + luck + combinedWithWeather;