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+
package com.anxpp.designpattern.abstractfactory;
2+
//抽象工厂模式
3+
public class AbstractFactory {
4+
public static void main(String args[]){
5+
IFactory bigfactory = new BigFactory();
6+
IFactory smallfactory = new BigFactory();
7+
bigfactory.producePhone().run();
8+
bigfactory.produceHeadset().play();
9+
smallfactory.producePhone().run();
10+
smallfactory.produceHeadset().play();
11+
}
12+
}
13+
//抽象产品*2
14+
interface Headset{
15+
void play();
16+
}
17+
//抽象产品
18+
interface MeizuPhone{
19+
void run();
20+
}
21+
//具体产品*2*2
22+
class PRO5 implements MeizuPhone{
23+
@Override
24+
public void run() {
25+
System.out.println("我是一台PRO5");
26+
}
27+
}
28+
class MX5 implements MeizuPhone{
29+
@Override
30+
public void run() {
31+
System.out.println("我是一台MX5");
32+
}
33+
}
34+
class EP21 implements Headset{
35+
@Override
36+
public void play() {
37+
System.out.println("我是一副EP21");
38+
}
39+
}
40+
class EP30 implements Headset{
41+
@Override
42+
public void play() {
43+
System.out.println("我是一台EP30");
44+
}
45+
}
46+
//抽象工厂
47+
interface IFactory{
48+
MeizuPhone producePhone();
49+
Headset produceHeadset();
50+
}
51+
//具体工厂*2
52+
class BigFactory implements IFactory{
53+
@Override
54+
public MeizuPhone producePhone() {
55+
return new PRO5();
56+
}
57+
@Override
58+
public Headset produceHeadset() {
59+
return new EP30();
60+
}
61+
}
62+
//具体工厂*2
63+
class SmallFactory implements IFactory{
64+
@Override
65+
public MeizuPhone producePhone() {
66+
return new MX5();
67+
}
68+
@Override
69+
public Headset produceHeadset() {
70+
return new EP21();
71+
}
72+
}

0 commit comments

Comments
 (0)