Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to arrange this classes?
    primarykey
    data
    text
    <p>I'm doing a simple project to manage data for a tabletop game, but I'm mostly using it to get experience about correct coding.</p> <p>I've just reached a point where I have five classes that are tightly coupled, and I'm not sure whether leaving the whole thing as it is, or refactoring.</p> <p>What I have is basically this:</p> <ul> <li>class <code>ShipTemplate</code>: This class ( that has nothing to do with c++ templates ) has all constant members and contains basic informations about a category of Ships.</li> <li>class <code>TemplateSet</code>: This class contains all ShipTemplates that are currently available to build, and it has a name. It should be stand-alone, since it represents the available technology of each player at any time, so one would be able to save/load different sets at different times.</li> <li>class <code>Ship</code>: This class represents a complete ship, with loadouts, name and other things. It contains a const reference to a ShipTemplate, which the class is not allowed to change, to refer to its basic functionality. It <em>could</em> extend ShipTemplate, but I wanted to keep track of which Ships had a particular underlying ShipTemplate, and it seemed easier doing it like this.</li> <li>class <code>Fleet</code>: This class contains a list of Ships, it has a name and contains other information. It should contain a cost variable equal to the sum of the cost of all Ships in it.</li> <li>class <code>Deployment</code>: This class contains pointers to all the Ships, Fleets, and TemplateSets available to the player. It also needs to keep track of ShipTemplates that are no longer available, but that are still used by already built Ships. It should contain a cost variable equal to the sum of the cost of all Ships available to the Player. It has to manage the transfer of Ships from one Fleet to another. It has to find out which Ships are within a given Fleet, or which Ships have a given ShipTemplate.</li> </ul> <p>Unfortunately every class is pretty interwined with all the others. I thought about different approaches, but I'm not sure if even one of them is the correct one.</p> <ul> <li>Use <code>friend</code> statements all over the place, so that if one class modifies something, it can correctly update all the others.</li> <li>Use very long names, like <code>Deployment::modifyShipThrustInFleet</code>, and allow any modification solely through the Deployment class, which will take care of everything.</li> <li>Remove TemplateSets and Fleets and represent them within Deployment, so that it can update correctly cost values/pointers without breaking any "correctness" rule. This too implies that every modification to the system has to pass through Deployment.</li> <li>Insert into lower classes pointers to upper classes, so for example when changing something in a Ship it can automatically update costs of both its Deployment and Fleet.</li> </ul> <p>Is there some other solution I didn't see, or maybe a refactor into more classes that can help me achieve readable, mantainable code?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload