|
| 1 | + |
| 2 | +class Program |
| 3 | +{ |
| 4 | + static void Main(string[] args) |
| 5 | + { |
| 6 | + //Builder design pattern separates the concern of creating or building a complex object from its representation |
| 7 | + //so that the same construction process can create different representations |
| 8 | + |
| 9 | + Director director = new Director(); |
| 10 | + |
| 11 | + Builder iphoneBuilder = new IphoneBuilder(); |
| 12 | + Builder samsungBuilder = new SamsungBuilder(); |
| 13 | + |
| 14 | + director.Construct(iphoneBuilder); |
| 15 | + CellPhone iphoneProduct = iphoneBuilder.GetResult(); |
| 16 | + iphoneProduct.Show(); |
| 17 | + |
| 18 | + director.Construct(samsungBuilder); |
| 19 | + CellPhone samsungProduct = samsungBuilder.GetResult(); |
| 20 | + samsungProduct.Show(); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +class Director |
| 26 | +{ |
| 27 | + public void Construct(Builder builder) |
| 28 | + { |
| 29 | + builder.BuildBattery(); |
| 30 | + builder.BuildScreen(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +abstract class Builder |
| 35 | +{ |
| 36 | + public abstract void BuildScreen(); |
| 37 | + public abstract void BuildBattery(); |
| 38 | + public abstract CellPhone GetResult(); |
| 39 | +} |
| 40 | + |
| 41 | +class IphoneBuilder : Builder |
| 42 | +{ |
| 43 | + private CellPhone cellPhone = new CellPhone(); |
| 44 | + |
| 45 | + public override void BuildScreen() |
| 46 | + { |
| 47 | + cellPhone.Add("Iphone Screen"); |
| 48 | + } |
| 49 | + public override void BuildBattery() |
| 50 | + { |
| 51 | + cellPhone.Add("Iphone Battery"); |
| 52 | + } |
| 53 | + public override CellPhone GetResult() |
| 54 | + { |
| 55 | + return cellPhone; |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +class SamsungBuilder : Builder |
| 61 | +{ |
| 62 | + private CellPhone cellPhone = new CellPhone(); |
| 63 | + |
| 64 | + public override void BuildBattery() |
| 65 | + { |
| 66 | + cellPhone.Add("Samsung Battery"); |
| 67 | + } |
| 68 | + public override void BuildScreen() |
| 69 | + { |
| 70 | + cellPhone.Add("Samsung Screen"); |
| 71 | + } |
| 72 | + public override CellPhone GetResult() |
| 73 | + { |
| 74 | + return cellPhone; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +class CellPhone |
| 81 | +{ |
| 82 | + private List<string> parts = new List<string>(); |
| 83 | + |
| 84 | + public void Add(string part) |
| 85 | + { |
| 86 | + parts.Add(part); |
| 87 | + } |
| 88 | + |
| 89 | + public void Show() |
| 90 | + { |
| 91 | + Console.WriteLine("\n=============================="); |
| 92 | + Console.WriteLine(" CellPhone Parts \n"); |
| 93 | + foreach (string part in parts) |
| 94 | + { |
| 95 | + Console.WriteLine(part); |
| 96 | + } |
| 97 | + Console.WriteLine("=============================="); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments