I’m relatively new to Objective C coding, so please help me with this :
In my code, I have a class named GameData, which is a singleton, and it contains some methods, and it’s this class which manage the scoring system. My question is, should I create another class for the Scores that I would store in GameData, or should I keep it this way? I want to be sure because I have a lot of methods for the score management! THANKS!!
In general, if you find that you are creating a lot of methods in object A to deal with a specific value or specific set of values contained in A, the value or values should be composed into class B with all B-value associated methods.
Seeing as you’re noticing this issue yourself, you should probably move the values and methods associated with
Scoredata into a separate object.For the methods that you move, ask yourself if the method is something that a
Scoreshould know about, or something that theGameDatamanager should know about.For example, a
Scoreprobably shouldn’t know about the existence of other scores because it’sGameData‘s responsibility to manage the composition of those scores. Conversely, theGameDatamanager shouldn’t manually sort your scores based off of some internalScoremetric (such aspointsGainedorsecondsToFinish), but instead rely onScoreto implement a comparison method to other objects that makes sense toScore. By giving objects only the information that matters to them in this example, you can make it easy to change how aScoreranks to other scores without needing to modify theGameDataclass.