-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGames.java
More file actions
69 lines (56 loc) · 1.42 KB
/
Copy pathGames.java
File metadata and controls
69 lines (56 loc) · 1.42 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
package interfaces.factorymethod;
/**
* Created by WORK_WERT on 19.01.2017.
*/
// игра
interface Game {
boolean move();
}
// фабрика игр
interface GameFactory {
Game getGame(); // фабричный метод
}
// имплементация Game (Шашки)
class Checkers implements Game {
private int moves = 0;
private final int MOVES = 3;
@Override
public boolean move() {
System.out.println("Checkers move " + moves);
return ++moves != MOVES;
}
}
// имплементация GameFactory для шашек
class CheckersFactory implements GameFactory {
@Override
public Game getGame() {
return new Checkers();
}
}
// имплементация Game (Шахматы)
class Chess implements Game {
private int moves = 0;
private final int MOVES = 4;
@Override
public boolean move() {
System.out.println("Chess move " + moves);
return ++moves != MOVES;
}
}
// имплементаця GamgeFactory для шахмат
class ChessFactory implements GameFactory {
@Override
public Game getGame() {
return new Chess();
}
}
public class Games {
public static void playGame(GameFactory factory) {
Game s = factory.getGame();
while (s.move()) ;
}
public static void main(String[] args) {
playGame(new CheckersFactory());
playGame(new ChessFactory());
}
}