I had a class hierarchy setup like so:
public abstract class GameController
public abstract class Game
I wanted to use a generic GameController class so it takes specific Game subclasses, so I changed it to:
public abstract class GameController<GameType extends Game>
public abstract class Game
But then I also wanted to have a generic Game to take specific player subclasses, so I changed it to:
public abstract class GameController<GameType extends Game<PlayerType>, PlayerType extends Player>
public abstract class Game<PlayerType extends Player>
Is there a better way to lay this out so I don't have to declare PlayerType twice (in both the GameController subclass and the Game subclass)?
Edit:
GameController is a network class, it receives post and get requests, and translates them into something closer to what a game needs, then a subclass, say MyGameController, further translates the byte[] that was posted into a method call for a specific game class, say MyGame. Code sample:
GameController:
public abstract class GameController<GameType extends Game<PlayerType>, PlayerType extends Player> {
private Hashtable<String, GameType> games;
public MultiplayerMain() {
super();
games = new Hashtable<String, GameType>();
}
protected abstract GameType createGame(InputStream in, String gameId);
protected abstract PlayerType createPlayer(InputStream in, GameType game, String playerId);
protected abstract byte[] gameAction(InputStream in, GameType game, PlayerType player);
//Other stuff that calls createGame, createPlayer, and gameAction
}
Game:
public abstract class Game<PlayerType extends Player> {
private Hashtable <String, PlayerType> players = new Hashtable<String, PlayerType>();
public final String gameId;
public Game (String id) {
gameId = id;
}
public abstract byte[] getGameState();
public abstract byte[] getGameState(PlayerType player);
final PlayerType getPlayer(String userId) {
return players.get(userId);
}
final void addPlayer(PlayerType player) {
players.put(player.userId,player);
playerAdded(player);
}
final void removePlayer(PlayerType player) {
players.remove(player);
playerRemoved(player);
}
protected void playerAdded(PlayerType player) {}
protected void playerRemoved(PlayerType player) {}
}
MyGameController:
public class MyGameController extends GameController<MyGame,MyPlaer> {
protected MyGame createGame(InputStream in, String gameId) {
byte[] data = getInitializationData(in);
return new MyGame(gameId,data);
}
protected byte[] gameAction(InputStream in, MyGame game, Player player) {
byte[] data = getData(in);
MyGame.methodSpecificToMyGameClassBasedOnInputStream(data);
return game.getGameState(player);
}
protected Player createPlayer(InputStream in, MyGame game, String playerId) {
byte[] otherData = getOtherData(in,game)
return new MyPlayer(playerId,otherData);
}
}
MyGame:
public class MyGame extends Game<MyPlayer> {
public MyGame(String id) {
//...
}
public byte[] getGameState() {
//...
}
public byte[] getGameState(Player user) {
//...
}
public void methodSpecificToMyGameClassBasedOnInputStream(byte[] data) {
//...
}
}