-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathE9_19_GameFactory.java
More file actions
74 lines (57 loc) · 1.17 KB
/
Copy pathE9_19_GameFactory.java
File metadata and controls
74 lines (57 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package unit9;
/**
* 使用工厂方法来创建一个框架,它可以执行抛硬币和掷骰子功能。
*
* 以下代码只是一个框架,没有具体编写如何实现这两个功能。
*
* @author Administrator
*
*/
interface Game {
void play();
}
interface GameFactory {
Game getGame();
}
class RingOiler implements Game {
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("RingOiler play()");
}
}
class Throw implements Game {
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("Throw play()");
}
}
class RingOilerFactory implements GameFactory {
@Override
public Game getGame() {
// TODO Auto-generated method stub
return new RingOiler();
}
}
class ThrowFactory implements GameFactory {
@Override
public Game getGame() {
// TODO Auto-generated method stub
return new Throw();
}
}
public class E9_19_GameFactory {
public static void playGame(GameFactory g) {
Game game = g.getGame();
game.play();
}
public static void main(String[] args) {
playGame(new RingOilerFactory());
playGame(new ThrowFactory());
}
}
/*
* RingOiler play() Throw play()
*
*/