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+
package com.anxpp.designpattern.simplefactory;
2+
//演示简单工厂
3+
public class SimpleFactory {
4+
public static void main(String args[]) throws Exception{
5+
Factory factory = new Factory();
6+
factory.produce("PRO5").run();
7+
factory.produce("PRO6").run();
8+
}
9+
}
10+
//抽象产品
11+
interface MeizuPhone{
12+
void run();
13+
}
14+
//具体产品X2
15+
class PRO5 implements MeizuPhone{
16+
@Override
17+
public void run() {
18+
System.out.println("我是一台PRO5");
19+
}
20+
}
21+
class PRO6 implements MeizuPhone{
22+
@Override
23+
public void run() {
24+
System.out.println("我是一台PRO6");
25+
}
26+
}
27+
//工厂
28+
class Factory{
29+
MeizuPhone produce(String product) throws Exception{
30+
if(product.equals("PRO5"))
31+
return new PRO5();
32+
else if(product.equals("PRO6"))
33+
return new PRO6();
34+
throw new Exception("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+
package com.anxpp.designpattern.simplefactory;

0 commit comments

Comments
 (0)