-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGames.java
More file actions
54 lines (46 loc) · 1.1 KB
/
Games.java
File metadata and controls
54 lines (46 loc) · 1.1 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
//: innerclasses/Games.java
// Using the anonymous inner classes with the Game Framework
package com.innerclasses10;
interface Game { boolean move(); }
interface GameFactory { Game getGame(); }
class Checker implements Game {
private Checker() {}
private int moves = 0;
private static final int MOVE = 3;
public boolean move() {
System.out.println("Checker move: " + moves);
return ++moves != MOVE;
}
public static GameFactory gamefactory =
new GameFactory() {
public Game getGame() {
return new Checker();
}
};
}
class Chess implements Game {
private Chess() {}
private int moves = 0;
private static final int MOVE = 4;
public boolean move() {
System.out.println("Chess move: " + moves);
return ++moves != MOVE;
}
public static GameFactory gamefactory =
new GameFactory() {
public Game getGame() {
return new Chess();
}
};
}
public class Games {
public static void playGame(GameFactory fact) {
Game game = fact.getGame();
while(game.move()) {
}
}
public static void main(String[] args) {
playGame(Checker.gamefactory);
playGame(Chess.gamefactory);
}
}