This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameService.java
173 lines (147 loc) · 3.94 KB
/
GameService.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.gamingroom;
import java.util.ArrayList;
import java.util.List;
/**
* A singleton service for the game engine
*
* @author eric.slutz@snuh.edu
*
*/
public class GameService {
/**
* A list of the active games
*/
private static final List<Game> games = new ArrayList<Game>();
/**
* Holds the next game identifier
*/
private static long nextGameId = 1;
/**
* Holds the next team identifier
*/
private static long nextTeamId = 1;
/**
* Holds the next player identifier
*/
private static long nextPlayerId = 1;
/**
* Holds the single gameService instance
*/
private static GameService gameService = null;
/**
* Private constructor for singleton pattern
*/
private GameService() {
}
/*
Singleton Pattern Purpose and Characteristics
The purpose of the singleton design pattern is to ensure that only one instance of a class
can be created and used during the life of the program. Here the singleton pattern is used
by making the GameService constructor private. The only way to get an instance of GameService
is through the public getGameService static method. That method in turn checks to see if the
static field of gameService has been initialized yet. If it has, then return that already
created instance. If it hasn't, then create a new instance for use for the life of the program.
*/
/**
* Checks if gameService has been initialized
* If it has, return that instance
* Otherwise, create a new instance of GameService
*
* @return the single gameService instance
*/
public static GameService getInstance() {
// if not found, make a new GameService instance
if (gameService == null) {
gameService = new GameService();
}
// return the new/existing gameService instance to the caller
return gameService;
}
/**
* Construct a new game instance
*
* @param name the unique name of the game
* @return the game instance (new or existing)
*/
public Game addGame(String name) {
// a local game instance
Game game = null;
/*
Iterator Pattern Purpose and Characteristics
The purpose of the iterator pattern is to be able to go through each element of a list.
In this case, a for loop is being used basically saying, for each existingGame in the
list games, execute the code inside the loop.
*/
// if found, simply return the existing instance
for (Game existingGame : games) {
if (existingGame.getName().equals(name)) {
game = existingGame;
}
}
// if not found, make a new game instance and add to list of games
if (game == null) {
game = new Game(nextGameId++, name);
games.add(game);
}
// return the new/existing game instance to the caller
return game;
}
/**
* Returns the game instance with the specified id.
*
* @param id unique identifier of game to search for
* @return requested game instance
*/
public Game getGame(long id) {
// a local game instance
Game game = null;
// if found, simply assign that instance to the local variable
for (Game existingGame : games) {
if (existingGame.getId() == id) {
game = existingGame;
}
}
return game;
}
/**
* Returns the game instance with the specified name.
*
* @param name unique name of game to search for
* @return requested game instance
*/
public Game getGame(String name) {
// a local game instance
Game game = null;
// if found, simply assign that instance to the local variable
for (Game existingGame : games) {
if (existingGame.getName().equals(name)) {
game = existingGame;
}
}
return game;
}
/**
* Returns the number of games currently active
*
* @return the number of games currently active
*/
public int getGameCount() {
return games.size();
}
/**
* Returns the next team id
*
* @return the next team id
*/
public long getNextTeamId() {
return nextTeamId++;
}
/**
* Returns the next player id
*
* @return the next player id
*/
public long getNextPlayerId() {
return nextPlayerId++;
}
}