The previous section, we introduce a simple factory pattern and factory method pattern , but both models there are certain limitations, can only produce a certain kind of product at a certain type, if demand changes, there has been under the same type different products, such as cheese pizza is not only the different tastes, different in appearance exist. This time, the factory model is clearly not meet the requirements, how to do it? So we think DIP principle , it is not precisely in order to resolve this situation exists it? Next we introduce the abstract factory pattern:
1, defines an abstract factory model interface for creating or dependencies associated cluster objects, without indicating a specific category.
2, abstract factory model can be simple factory mode and integration mode factory method.
3, from the design level, the abstract factory pattern is to improve the simple factory pattern (ie further abstraction)
4, the abstract factory into two layers, and the specific implementation of the abstract factory plant.
Or take the pizza ordering example, we define an abstract factory AbsFactory, achieved by a subclass of the abstract factory factory; Order pizza OrderPizza rely on abstract is not dependent on the specific implementation.
1 internal class Program 2 { 3 private static void Main(string[] args) 4 { 5 new OrderPizza(new BJFactory()); 6 } 7 } 8 9 internal class OrderPizza 10 { 11 private AbsFactory factory; 12 13 public OrderPizza(AbsFactory factory) 14 { 15 setFactory(factory); 16 Order(); 17 } 18 19 private void setFactory(AbsFactory factory) 20 { 21 this.factory = factory; 22 } 23 24 private void Order() 25 { 26 Pizza pizza = null; 27 string orderType = ""; 28 do 29 { 30 Console.Write("请输入订购类型:"); 31 orderType = Console.ReadLine(); 32 pizza = this.factory.createPizza(orderType); 33 if (pizza == null) 34 { 35 Console.WriteLine("订购失败"); 36 break; 37 } 38 //开始制作 39 pizza.prepare(); 40 pizza.bake(); 41 pizza.cut(); 42 pizza.box(); 43 } while (true); 44 } 45 } 46 47 internal interface AbsFactory 48 { 49 Pizza createPizza(string orderType); 50 } 51 52 internal class BJFactory : AbsFactory 53 { 54 public Pizza createPizza(string orderType) 55 { 56 Pizza pizza = null; 57 if (orderType == "cheese") 58 { 59 Pizza = new new BJCheesePizza (); 60 pizza.setName ( " Beijing Cheese Pizza " ); 61 is } 62 is the else IF (OrderType == " greek " ) 63 is { 64 Pizza = new new BJGreekPizza (); 65 pizza.setName ( " Beijing Greece Pizza " ); 66 } 67 return Pizza; 68 } 69 } 70 71 internal class LDFactory : AbsFactory 72 { 73 public Pizza createPizza(string orderType) 74 { 75 Pizza pizza = null; 76 if (orderType == "cheese") 77 { 78 pizza = new LDCheesePizza(); 79 pizza.setName("伦敦芝士披萨"); 80 } 81 else if (orderType == "greek") 82 { 83 pizza = new LDGreekPizza(); 84 pizza.setName("伦敦希腊披萨"); 85 } 86 return pizza; 87 } 88 } 89 90 internal abstract class Pizza 91 { 92 private string name; 93 94 public abstract void prepare(); 95 96 public void bake() 97 { 98 Console.WriteLine($"{this.name} 烘培"); 99 } 100 101 public void cut() 102 { 103 Console.WriteLine($"{this.name} 修剪"); 104 } 105 106 public void box() 107 { 108 Console.WriteLine($"{this.name} 打包"); 109 } 110 111 public void the setName ( String name) 112 { 113 the this .name = name; 114 } 115 } 1 16 117 Internal class BJCheesePizza: Pizza 1 18 { 119 public the override void PREPARE () 120 { 121 Console.WriteLine ( " Beijing pizza cheese preparation in " ); 122 } 123 } 124 125 Internal class BJGreekPizza: Pizza 126 { 127 public the override void PREPARE () 128 { 129 Console.WriteLine ( " Greek pizza prepared in Beijing " ); 130. } 131 is } 132 133 Internal class LDCheesePizza: Pizza 134 { 135 public the override void PREPARE () 136 { 137 Console .WriteLine ( " cheese pizza preparation London " ); 138 } 139 } 140 141 is Internal class LDGreekPizza: Pizza 142 { 143 public the override void PREPARE () 144 { 145 Console.WriteLine ( " Greek pizza preparation London " ); 146 } 147 }
Read some bloggers and blog comments, some understanding is still pretty place:
1, abstract factory complex than the factory method, they have different purposes. Factory method is intended to delay loading, abstract methods are intended to the high cohesion and low coupling.
2, factory method model concrete factory can create an instance of a particular class of specific product type, and can create multiple abstract factory.
Reference: https://www.runoob.com/design-pattern/abstract-factory-pattern.html