Skip to content

Commit 07488dc

Browse files
committed
工厂方法模式
1 parent 7b9108d commit 07488dc

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.anxpp.designpattern.factorymethod;
2+
//工厂方法模式
3+
public class FactoryMethod {
4+
public static void main(String args[]){
5+
IFactory bigfactory;
6+
bigfactory = new SmallFactory();
7+
bigfactory.produce().run();
8+
bigfactory = new BigFactory();
9+
bigfactory.produce().run();
10+
}
11+
}
12+
//抽象产品
13+
interface MeizuPhone{
14+
void run();
15+
}
16+
//具体产品*2
17+
class PRO5 implements MeizuPhone{
18+
@Override
19+
public void run() {
20+
System.out.println("我是一台PRO5");
21+
}
22+
}
23+
class MX5 implements MeizuPhone{
24+
@Override
25+
public void run() {
26+
System.out.println("我是一台MX5");
27+
}
28+
}
29+
interface IFactory{
30+
MeizuPhone produce();
31+
}
32+
//工厂*2
33+
class BigFactory implements IFactory{
34+
@Override
35+
public MeizuPhone produce() {
36+
return new PRO5();
37+
}
38+
}
39+
class SmallFactory implements IFactory{
40+
@Override
41+
public MeizuPhone produce() {
42+
return new MX5();
43+
}
44+
}

src/com/anxpp/designpattern/factorymethod/Iterator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.anxpp.designpattern.factorymethod;
2-
32
//只是需要遍历一堆数据,那么只需要2个方法就可以了
43
public interface Iterator<T> {
54
boolean hasNext(); //是否还有下一个元素

0 commit comments

Comments
 (0)