|
12 | 12 |
|
13 | 13 | 为了更清晰地理解工厂方法模式,需要先引入两个概念:
|
14 | 14 |
|
15 |
| -**产品等级结构 :**产品等级结构即产品的继承结构,如一个抽象类是电视机,其子类有海尔电视机、海信电视机、TCL电视机,则抽象电视机与具体品牌的电视机之间构成了一个产品等级结构,抽象电视机是父类,而具体品牌的电视机是其子类。 |
| 15 | +**产品等级结构:** 产品等级结构即产品的继承结构,如一个抽象类是电视机,其子类有海尔电视机、海信电视机、TCL电视机,则抽象电视机与具体品牌的电视机之间构成了一个产品等级结构,抽象电视机是父类,而具体品牌的电视机是其子类。 |
16 | 16 |
|
17 |
| -**产品族 :**在抽象工厂模式中,**产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品**,如海尔电器工厂生产的海尔电视机、海尔电冰箱,海尔电视机位于电视机产品等级结构中,海尔电冰箱位于电冰箱产品等级结构中。 |
| 17 | +**产品族:** 在抽象工厂模式中,**产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品**,如海尔电器工厂生产的海尔电视机、海尔电冰箱,海尔电视机位于电视机产品等级结构中,海尔电冰箱位于电冰箱产品等级结构中。 |
18 | 18 |
|
19 | 19 | **当系统所提供的工厂所需生产的具体产品并不是一个简单的对象,而是多个位于不同产品等级结构中属于不同类型的具体产品时需要使用抽象工厂模式。**
|
20 | 20 |
|
@@ -59,136 +59,133 @@ Product:具体产品
|
59 | 59 | **抽象产品: 苹果系列**
|
60 | 60 |
|
61 | 61 | ```java
|
62 |
| - public interface Apple |
63 |
| - { |
64 |
| - void AppleStyle(); |
65 |
| - } |
| 62 | +public interface Apple |
| 63 | +{ |
| 64 | + void AppleStyle(); |
| 65 | +} |
66 | 66 | ```
|
67 | 67 |
|
68 | 68 | **抽象产品: 三星系列**
|
69 | 69 |
|
70 | 70 | ```java
|
71 | 71 | public interface Sumsung
|
72 |
| - { |
73 |
| - void BangziStyle(); |
74 |
| - } |
| 72 | +{ |
| 73 | + void BangziStyle(); |
| 74 | +} |
75 | 75 | ```
|
76 | 76 |
|
77 | 77 | **具体产品:iphone**
|
78 | 78 |
|
79 | 79 | ```java
|
80 |
| - public class iphone implements Apple |
81 |
| - { |
82 |
| - public void AppleStyle() |
83 |
| - { |
84 |
| - Console.WriteLine("Apple's style: iPhone!"); |
85 |
| - } |
86 |
| - } |
| 80 | +public class iphone implements Apple |
| 81 | +{ |
| 82 | + public void AppleStyle() |
| 83 | + { |
| 84 | + Console.WriteLine("Apple's style: iPhone!"); |
| 85 | + } |
| 86 | +} |
87 | 87 | ```
|
88 | 88 |
|
89 | 89 | **具体产品:ipad**
|
90 | 90 |
|
91 | 91 | ```java
|
92 |
| - public class ipad implements Apple |
| 92 | +public class ipad implements Apple |
| 93 | +{ |
| 94 | + public void AppleStyle() |
93 | 95 | {
|
94 |
| - |
95 |
| - public void AppleStyle() |
96 |
| - { |
97 |
| - Console.WriteLine("Apple's style: iPad!"); |
98 |
| - } |
99 |
| - |
100 |
| - } |
| 96 | + Console.WriteLine("Apple's style: iPad!"); |
| 97 | + } |
| 98 | +} |
101 | 99 | ```
|
102 | 100 |
|
103 | 101 | **具体产品:note2**
|
104 | 102 |
|
105 | 103 | ```java
|
106 |
| - public class note2 implements Sumsung |
107 |
| - { |
108 |
| - public void BangziStyle() |
109 |
| - { |
110 |
| - Console.WriteLine("Bangzi's style : Note2!"); |
111 |
| - } |
112 |
| - |
113 |
| - } |
| 104 | +public class note2 implements Sumsung |
| 105 | +{ |
| 106 | + public void BangziStyle() |
| 107 | + { |
| 108 | + Console.WriteLine("Bangzi's style : Note2!"); |
| 109 | + } |
| 110 | +} |
114 | 111 | ```
|
115 | 112 |
|
116 | 113 | **具体产品:tabs**
|
117 | 114 |
|
118 | 115 | ```java
|
119 | 116 | public class Tabs implements Sumsung
|
120 |
| - { |
121 |
| - public void BangziStyle() |
122 |
| - { |
123 |
| - Console.WriteLine("Bangzi's style : Tab!"); |
124 |
| - } |
125 |
| - } |
| 117 | +{ |
| 118 | + public void BangziStyle() |
| 119 | + { |
| 120 | + Console.WriteLine("Bangzi's style : Tab!"); |
| 121 | + } |
| 122 | +} |
126 | 123 | ```
|
127 | 124 |
|
128 | 125 | **抽象工厂**
|
129 | 126 |
|
130 | 127 | ```java
|
131 |
| - public interface Factory |
132 |
| - { |
133 |
| - Apple createAppleProduct(); |
134 |
| - Sumsung createSumsungProduct(); |
135 |
| - } |
| 128 | +public interface Factory |
| 129 | +{ |
| 130 | + Apple createAppleProduct(); |
| 131 | + Sumsung createSumsungProduct(); |
| 132 | +} |
136 | 133 | ```
|
137 | 134 |
|
138 | 135 | **手机工厂**
|
139 | 136 |
|
140 | 137 | ```java
|
141 | 138 | public class Factory_Phone implements Factory
|
142 |
| - { |
143 |
| - public Apple createAppleProduct() |
144 |
| - { |
145 |
| - return new iphone(); |
146 |
| - } |
147 |
| - |
148 |
| - public Sumsung createSumsungProduct() |
149 |
| - { |
150 |
| - return new note2(); |
151 |
| - } |
152 |
| - } |
| 139 | +{ |
| 140 | + public Apple createAppleProduct() |
| 141 | + { |
| 142 | + return new iphone(); |
| 143 | + } |
| 144 | + |
| 145 | + public Sumsung createSumsungProduct() |
| 146 | + { |
| 147 | + return new note2(); |
| 148 | + } |
| 149 | +} |
153 | 150 | ```
|
154 | 151 |
|
155 | 152 | **pad工厂**
|
156 | 153 |
|
157 | 154 | ```java
|
158 | 155 | public class Factory_Pad implements Factory
|
159 |
| - { |
160 |
| - public Apple createAppleProduct() |
161 |
| - { |
162 |
| - return new ipad(); |
163 |
| - } |
164 |
| - |
165 |
| - public Sumsung createSumsungProduct() |
166 |
| - { |
167 |
| - return new Tabs(); |
168 |
| - } |
169 |
| - } |
| 156 | +{ |
| 157 | + public Apple createAppleProduct() |
| 158 | + { |
| 159 | + return new ipad(); |
| 160 | + } |
| 161 | + |
| 162 | + public Sumsung createSumsungProduct() |
| 163 | + { |
| 164 | + return new Tabs(); |
| 165 | + } |
| 166 | +} |
170 | 167 | ```
|
171 | 168 |
|
172 | 169 | **客户端调用**
|
173 | 170 |
|
174 | 171 | ```java
|
175 | 172 | public static void Main(string[] args)
|
176 |
| - { |
177 |
| - //采购商要一台iPad和一台Tab |
178 |
| - Factory factory = new Factory_Pad(); |
179 |
| - Apple apple = factory.createAppleProduct(); |
180 |
| - apple.AppleStyle(); |
181 |
| - Sumsung sumsung = factory.createSumsungProduct(); |
182 |
| - sumsung.BangziStyle(); |
183 |
| - |
184 |
| - //采购商又要一台iPhone和一台Note2 |
185 |
| - factory = new Factory_Phone(); |
186 |
| - apple = factory.createAppleProduct(); |
187 |
| - apple.AppleStyle(); |
188 |
| - sumsung = factory.createSumsungProduct(); |
189 |
| - sumsung.BangziStyle(); |
190 |
| - Console.ReadKey(); |
191 |
| - } |
| 173 | +{ |
| 174 | + //采购商要一台iPad和一台Tab |
| 175 | + Factory factory = new Factory_Pad(); |
| 176 | + Apple apple = factory.createAppleProduct(); |
| 177 | + apple.AppleStyle(); |
| 178 | + Sumsung sumsung = factory.createSumsungProduct(); |
| 179 | + sumsung.BangziStyle(); |
| 180 | + |
| 181 | + //采购商又要一台iPhone和一台Note2 |
| 182 | + factory = new Factory_Phone(); |
| 183 | + apple = factory.createAppleProduct(); |
| 184 | + apple.AppleStyle(); |
| 185 | + sumsung = factory.createSumsungProduct(); |
| 186 | + sumsung.BangziStyle(); |
| 187 | + Console.ReadKey(); |
| 188 | +} |
192 | 189 | ```
|
193 | 190 |
|
194 | 191 | 抽象工厂可以通过多态,来动态设置不同的工厂,生产不同的产品,同时每个工厂中的产品又不属于同一个产品等级结构。
|
|
0 commit comments