简体   繁体   中英

iOS Design Pattern Implementation

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.

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, eg 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM