I have a Game class that needs some resources that are produced by Generators classes. Every game has its own generators and because generators are heavy objects to instantiate, Game keeps an object pool with instances. In every Generator I have methods like these:
private MapBoardGenerator(Game game) {
super(game);
}
public static MapBoardGenerator getInstance(Game game) {
MapBoardGenerator instance = game.getGenerator(MapBoardGenerator.class);
if (instance == null) {
instance = new MapBoardGenerator(game);
game.addGenerator(MapBoardGenerator.class, instance);
}
return instance;
}
This static method is almost identical in every class that extends Generator.
What I want to do is provide a Supplier<MapBoardGenerator> to game so the controls will be made elsewhere and getInstance method will simply be:
public static MapBoardGenerator getInstance(Game game) {
return game.getInstance(MapBoardGenerator.class, MapBoardGenerator::new);
}
Is something wrong if I pass a supplier that calls a private constructor? It's a university project and design matters a lot here.
Aucun commentaire:
Enregistrer un commentaire