2

I'm struggling with design patterns and would like to follow best practices while rewriting my current app from Objective-C to Swift.

I have a game where a player gets extra scores/or special prizes for, let's say, solving a certain amount of levels. There are several milestones to be reached (such as 10 levels completed/50 levels completed). I have different game modes, so I have several subclasses of a Gameplay class. After each player's turn -checkSolution is being invoked to check whether the player solved the level. In case of success I increment the player's games score and send him to a new level . And here I also call -checkForMilestone of ScoreManager class to check whether any of the milestones has been reached.

I'm not sure whether it is ok to call -checkForMilestone inside -checkSolution. Or it might be better to create a callback using blocks or use KVO to observe (from ScoreManager class) whether the player's score was changed and then react appropriately.

autobot
  • 139
  • 9

2 Answers2

1

I would keep things simple. You can use KVO, events or other methods but from your description I can't see any benefit but adding complexity to the code and making things harder to debug. If you are already calling checkSolution() and you know this is the only place that will trigger a milestone change, then you should keep these two methods tight together. If on the other hand, milestones can be decoupled from successful solutions, e.g player buying them with micro-payments or friends contributions adding to the player's milestones, then you can create an observable pattern on the score or whatever triggers things to get updated.

John Difool
  • 5,572
  • 5
  • 45
  • 80
  • Thanks. I guess I should go with Observer then. I'm just aware of turning my view controller into massive view controller in future :) – autobot Jun 27 '15 at 21:41
1

First, good on you for even considering this. The key concepts involved in the decision are "scope of responsibility" and "separation of concerns".

You can likely determine the better option by considering:

  1. What events could trigger a milestone being reached?
  2. What class is responsible for that action?
  3. Could a milestone be achieved separately from a level being solved?
picciano
  • 22,341
  • 9
  • 69
  • 82
  • Well, answering the questions above I would say: 1. For on of the game modes it is "solving a level". For another game mode it is "prodviding a certain amount of correct answers". So, 2 different methods of 2 slightly different Gameplay class subclasses can trigger a milestone being reached. 2. Gameplay subclass is responsible for `-checkSolution` and ScoreManager class is responsible for total score calculation and checking whether a milestone was reached. 3. Yes. See answer 1. – autobot Jun 27 '15 at 21:34
  • I'm familiar with SRP term, anyway it's hard to come up with the best solution. I wouldn't bother myself thinking about such stuff in e.g. PHP, but here in iOS I see a wide range of sweet stuff I could use instead of just calling methods of another class instance whenever I want :) – autobot Jun 27 '15 at 21:39
  • Thinking about it another way, "who knows about the change that could affect a milestone and when do they know it?" – picciano Jun 27 '15 at 23:15