Intermediate Design Patterns in Swift

Design patterns are incredibly useful for making code maintainable and readable. Learn design patterns in Swift with this hands on tutorial. By .

Leave a rating/review
Save for later
Share
You are currently viewing page 6 of 6 of this article. Click here to view the first page.

Where To Go From Here?

Here is the final completed project for this tutorial.

You've taken a fun game, Tap the Larger Shape and used design patterns to add even more shapes and enhance their styling. You've also used design patterns to add more elaborate scoring.

Most remarkably, even though the final project has many more features, its code is actually simpler and more maintainable than what you started with.

Why not use these design patterns to extend your game even further? Some ideas follow.

Add more shapes like triangle, parallelogram, star, etc
Hint: Think back to how you added circles, and follow a similar sequence of steps to add new shapes. If you come up with some really cool shapes, please post screenshots of them in the comments at the bottom of this tutorial!

Add an animation whenever the score changes
Hint: Use the didSet property observer on GameView.score.

Add controls so that players can choose which types of shapes the game uses
Hint: Add three UIButton or a UISegmentedControl with three choices (Square, Circle, Mixed) in GameView, which should forward any target actions from the controls on to an Observer (Swift closure). GameViewController can use these closures to adjust which TurnStrategy it uses.

Persist shape settings to preferences that you can restore
Hint: Store the player's choice of shape type in NSUserDefaults. Try to use the Facade design pattern (Facade details) to hide your choice of persistence mechanism for this preference from the rest of your code.

Allow players to select the color scheme for the game
Hint: Use NSUserDefaults to persist the player's choice. Create a ShapeViewBuilder that can accept the persisted choice and adjust the app's UI accordingly. Could you use NSNotificationCenter to inform all interested views that the color scheme changed so that they can immediately update themselves?

Have your game play a happy sound when a match occurs and a sad sound when a match fails
Hint: Extend the Observer pattern used between GameView and GameViewController.

Use Dependency Injection to pass in the Scorer to TurnController
Hint: Remove the hard-coded dependency on MatchScorer and StreakScorer from the initializer.

Thank you for working through this tutorial! Please join the discussion below and share your questions, ideas and cool ways you kicked the game up a few notches.