(转自http://joe5456536.blog.163.com/blog/static/853747732011511104932500/)注:抽象工厂与简单工厂或工厂方法模式的区别:简单工厂或工厂方法模式更关注单个产品对象的创建,比如CPU工厂,它只关心如何创建CPU的对像,主板工 厂只关心如何创建主板对象。而抽象工厂模式是把正确的CPU与正确的主板进行匹配,把他们绑定在一起,对外提供一个接口,使客户端无需关心具体实现。
以下为自己心得。
首先上面的一句话很好的搞清了这个抽象工厂和工厂的区别YU联系! 我的理解是 从工厂模式里面生产一些零件,然后到抽象工厂中进行组装! 这样产生了一系列的东西!
我造一个华硕厂和联想厂。 他生产电脑,电脑的组件都是有其他的供货商提供,华硕和联想只是组装起来成一个电脑,然后卖出去!
1 package zzuli.acmen.abstractfactory;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 /**
7 * 生产AbstractFactory的抽象类
8 * 目标组装一台电脑吧!
9 * @author Acmen
10 *
11 */
12 public interface AbstractFactory {
13
14 public Computer getComputer();
15
16 }
17
18 //联想厂子
19 class LenoveFactory implements AbstractFactory{
20
21 private Factory cpuF = new CPUFactory(),memoryF = new MemoryFactory();
22
23 public Computer getComputer() {
24
25 CPU cpu = (CPU)cpuF.getObject(CPUFactory.str+"IntelCPU");
26 Memory memory = (Memory)memoryF.getObject(MemoryFactory.str+"MyMemory");
27
28 Computer com = new Lenove(cpu,memory);
29 com.setName("联想");
30
31 return com;
32 }
33
34
35
36 }
37
38 //华硕厂子
39 class AsusFactory implements AbstractFactory{
40
41 private Factory cpuF = new CPUFactory(),memoryF = new MemoryFactory();
42
43 public Computer getComputer() {
44
45 CPU cpu = (CPU)cpuF.getObject(CPUFactory.str+"AMDCPU");
46 Memory memory = (Memory)memoryF.getObject(MemoryFactory.str+"MyMemory");
47
48 Computer com = new Lenove(cpu,memory);
49 com.setName("华硕");
50
51 return com;
52 }
53
54 }
55
56 abstract class Computer{
57
58 String name;
59 CPU cpu;
60 Memory memory;
61
62 public abstract void show();
63 public Computer(CPU cpu,Memory memory){
64 this.cpu = cpu;
65 this.memory = memory;
66 }
67
68 public abstract void setName(String str);
69
70 }
71
72 /*
73 * 各个厂商的CPU
74 */
75 interface CPU{
76
77 public void say();
78
79 }
80
81 class IntelCPU implements CPU{
82
83 public void say() {
84 // TODO Auto-generated method stub
85 System.out.println("我是Intel的CPU");
86 }
87
88 }
89
90 class AMDCPU implements CPU{
91
92 public void say() {
93
94 System.out.println("我是AMD的CPU");
95
96 }
97
98
99 }
100
101 /*
102 * 各个厂商的内存
103 */
104
105 interface Memory{
106
107 public void say();
108
109 }
110
111 class KingstonMemory implements Memory{
112
113 public void say() {
114
115 System.out.println("我是金士顿的内存条");
116
117 }
118
119 }
120
121 class MyMemory implements Memory{
122
123 public void say() {
124
125 System.out.println("我是自己造的内存条");
126
127 }
128
129 }
130
131 //联想电脑
132
133 class Lenove extends Computer{
134
135 public Lenove(CPU cpu, Memory memory) {
136 super(cpu, memory);
137
138 }
139
140 public void show() {
141 System.out.println("我是"+name+"的电脑");
142 cpu.say();
143 memory.say();
144
145 }
146
147 public void setName(String str){
148 this.name = str;
149 }
150
151
152 }
153
154 //华硕电脑
155
156 class Asus extends Computer{
157
158 public Asus(CPU cpu, Memory memory) {
159 super(cpu, memory);
160
161 }
162
163 @Override
164 public void setName(String str) {
165 this.name = str;
166
167 }
168
169 @Override
170 public void show() {
171 System.out.println("我是"+name+"的电脑");
172 cpu.say();
173 memory.say();
174
175 }
176
177 }
178
179 /*
180 * 单个组件的生产工厂
181 */
182 class Factory {
183
184 Map<String,Object> map = new HashMap<String,Object>();
185
186 public void setObject(String str){
187
188 //反射机制
189 try {
190 /*
191 * 很关键的几行代码。因为我实现了根据字符串生成Object对象
192 */
193 Class c = Class.forName(str);
194 Object o = c.newInstance();
195 map.put(str, o);
196 } catch (ClassNotFoundException e) {
197 // TODO Auto-generated catch block
198 e.printStackTrace();
199 } catch (InstantiationException e) {
200 // TODO Auto-generated catch block
201 e.printStackTrace();
202 } catch (IllegalAccessException e) {
203 // TODO Auto-generated catch block
204 e.printStackTrace();
205 }
206
207
208 }
209
210 public Object getObject(String str){
211
212 Object o = map.get(str);
213
214 return o;
215
216 }
217 }
218 //CPU工厂
219 class CPUFactory extends Factory{
220
221 static final String str = "zzuli.acmen.abstractfactory.";
222
223 public CPUFactory() {
224 this.setObject(str+"IntelCPU");
225 this.setObject(str+"AMDCPU");
226 }
227
228 }
229
230 //Memory工厂
231 class MemoryFactory extends Factory{
232
233 static final String str = "zzuli.acmen.abstractfactory.";
234 public MemoryFactory(){
235 this.setObject(str+"KingstonMemory");
236 this.setObject(str+"MyMemory");
237 }
238
239 }
240
241 //测试用例了
242 class Main{
243
244 public static void main(String[] args){
245
246 AbstractFactory lenove_Factory = new LenoveFactory();
247 AbstractFactory asus_Factory = new AsusFactory();
248
249 Computer lenove_Computer = lenove_Factory.getComputer();
250 Computer asus_Computer = asus_Factory.getComputer();
251
252 lenove_Computer.show();
253 asus_Computer.show();
254
255 }
256
257 }