In my real-time multiplayer game, there are multiple entities that are very interrelated and whose logic is very related to the player who owns them. I don't think I can decouple the classes from each other, but I would like to decouple this group of classes from the Player who owns some of these entities. The Player as I see it is a high level object that interacts with the game map and the web socket server. Here is the context:
Every
Playeris an airline that managesAirplanes that transportPassengers betweenTerminals.A
Terminalis contained in anAirportand simply represents the "portion" of the airport designated for the player.
Responsibilities of each class:
Airport: assign(Passenger) to an Airplane in one of the terminals. (Thus, must contain the collection of Terminals)
Terminal: requestFlight(Airplane). Keeps track of capacity of the terminal (each player has a certain number of airplanes allowed in the terminal which can go up throughout the game).
Airplane: Maintains its own internal state of either in PASSENGER_EXCHANGE mode or IN_FLIGHT. When in passenger exchange mode you can add passengers to the airplane but not when in flight. When in passenger exchange, it is at a particular airport and terminal, so the Terminal must contain that Airport.
Passenger: Maintains its destination and connections of airports to the destination. Does NOT keep track of what Airplane it is on.
I would like to test the Airport logic without needing to instantiate or mock a Player, which takes too many high-level objects for a unit test. But adding a terminal to an airport requires a player so that I know that that player doesn't already have a terminal.
Alternatives designs to having the airports contain terminals and terminals take in players I have considered:
Player contains its terminals (already does). Terminals contain airports. This doesn't work because airport logic depends on access to terminals (assigning passengers to terminals)
Interface like
Ownerthat I can mock. This also doesn't seem like a good solution because thePlayerobject is used to call theequals()method.Refactor high-level
Playerobject so it does not depend onGameMap. I would like to do this, and it would make testing possible, but the player is directly manipulating the game map, so I don't know how I can write any of the game logic without access to the game map.
equals()method in thePlayer, is it really simple or does it involve something more? Cos it seems like you should "cut the middle man" and follow DavidT's suggestion. \$\endgroup\$