Generic generic class in Java

I had a hierarchical class hierarchy:

public abstract class GameController
public abstract class Game

I wanted to use the generic GameController class, so it accepts certain subclasses of Game, so I changed it to:

public abstract class GameController<GameType extends Game>
public abstract class Game

But then I also wanted the generic game to accept certain subclasses of players, 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 spell it out, so I don't need to declare PlayerType twice (both in the GameController subclass and in the Game subclass)?

Edit

A GameController is a network class, it receives messages and receives requests and translates them into something closer to what the player needs, and then a subclass, such as MyGameController, also translates the byte [] that was sent to the method call for a specific game class, let's say MyGame. Code example:

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
}

A 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) {
        //...
    }
}
+3
3

? Player Game Game GameController?

public class Game {
    private final PlayerType playerType;

    public Game(PlayerType playerType) {...}

}

public class GameController {
    private final Game game;

    public GameController(Game game) {...}
}
+3

, PlayerType . , :

public abstract class GameController<GameType extends Game<?>>
public abstract class Game<PlayerType extends Player>

gameController.getPlayer() Player, gameController.getGame(). getPlayer(), PlayerType.

: , , , Player .

, , . , gameAction , .

0

It looks like you really strongly associate the definition of GameController with the inheritance hierarchy of Game and GameType; similar to Game to Player and PlayerType. I would advise you to relate to these relationships much more freely; composition, as other posters suggest, is certainly the right way because you are describing a has-a relationship.

0
source

Source: https://habr.com/ru/post/1733578/


All Articles