lundi 11 janvier 2016

Factory dependency in C++ classes

Consider there are two classes, named User and Player. These classes, superficially, refer to users (accounts) in a game and players of them. Each user has one player, whereas each player could be owned by single user.

When user wants to join a game, a Player instance is created and the game abstraction will own the including players.

The instances of class Player should not be created by anyone other than a User. To be more precise, User has a method Player *createPlayer(...) in order to create a Player.

How this could be achieved? I considered User class could inherit from Player, so with a protected constructor of Player it will be okay. However, you know, this is ultimately odd. We would like to inherit Player from User, not the opposite.

Aucun commentaire:

Enregistrer un commentaire