Skip to content

Commit 2e69331

Browse files
committed
抽象工厂模式
1 parent fb6bf03 commit 2e69331

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
packagecom.anxpp.designpattern.abstractfactory;
2+
//抽象工厂模式
3+
publicclassAbstractFactory{
4+
publicstaticvoidmain(Stringargs[]){
5+
IFactorybigfactory = newBigFactory();
6+
IFactorysmallfactory = newBigFactory();
7+
bigfactory.producePhone().run();
8+
bigfactory.produceHeadset().play();
9+
smallfactory.producePhone().run();
10+
smallfactory.produceHeadset().play();
11+
}
12+
}
13+
//抽象产品*2
14+
interfaceHeadset{
15+
voidplay();
16+
}
17+
//抽象产品
18+
interfaceMeizuPhone{
19+
voidrun();
20+
}
21+
//具体产品*2*2
22+
classPRO5implementsMeizuPhone{
23+
@Override
24+
publicvoidrun(){
25+
System.out.println("我是一台PRO5");
26+
}
27+
}
28+
classMX5implementsMeizuPhone{
29+
@Override
30+
publicvoidrun(){
31+
System.out.println("我是一台MX5");
32+
}
33+
}
34+
classEP21implementsHeadset{
35+
@Override
36+
publicvoidplay(){
37+
System.out.println("我是一副EP21");
38+
}
39+
}
40+
classEP30implementsHeadset{
41+
@Override
42+
publicvoidplay(){
43+
System.out.println("我是一台EP30");
44+
}
45+
}
46+
//抽象工厂
47+
interfaceIFactory{
48+
MeizuPhoneproducePhone();
49+
HeadsetproduceHeadset();
50+
}
51+
//具体工厂*2
52+
classBigFactoryimplementsIFactory{
53+
@Override
54+
publicMeizuPhoneproducePhone(){
55+
returnnewPRO5();
56+
}
57+
@Override
58+
publicHeadsetproduceHeadset(){
59+
returnnewEP30();
60+
}
61+
}
62+
//具体工厂*2
63+
classSmallFactoryimplementsIFactory{
64+
@Override
65+
publicMeizuPhoneproducePhone(){
66+
returnnewMX5();
67+
}
68+
@Override
69+
publicHeadsetproduceHeadset(){
70+
returnnewEP21();
71+
}
72+
}

0 commit comments

Comments
(0)