Skip to content

Commit 5cc7f36

Browse files
committed
简单工厂模式
1 parent 07488dc commit 5cc7f36

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagecom.anxpp.designpattern.simplefactory;
2+
//演示简单工厂
3+
publicclassSimpleFactory{
4+
publicstaticvoidmain(Stringargs[]) throwsException{
5+
Factoryfactory = newFactory();
6+
factory.produce("PRO5").run();
7+
factory.produce("PRO6").run();
8+
}
9+
}
10+
//抽象产品
11+
interfaceMeizuPhone{
12+
voidrun();
13+
}
14+
//具体产品X2
15+
classPRO5implementsMeizuPhone{
16+
@Override
17+
publicvoidrun(){
18+
System.out.println("我是一台PRO5");
19+
}
20+
}
21+
classPRO6implementsMeizuPhone{
22+
@Override
23+
publicvoidrun(){
24+
System.out.println("我是一台PRO6");
25+
}
26+
}
27+
//工厂
28+
classFactory{
29+
MeizuPhoneproduce(Stringproduct) throwsException{
30+
if(product.equals("PRO5"))
31+
returnnewPRO5();
32+
elseif(product.equals("PRO6"))
33+
returnnewPRO6();
34+
thrownewException("No Such Class");
35+
}
36+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @author Administrator
3+
* 简单工厂模式
4+
*/
5+
packagecom.anxpp.designpattern.simplefactory;

0 commit comments

Comments
(0)