Fundamental Design Pattern: Strategy

Introduction

Design Patterns that will be covered in this article:

  • Strategy

Strategy

This design pattern defines a family of interchangeable objects that can be set or switched at runtime. Also, Strategy is categorized under Behavioral patterns because the Strategy pattern is ultimately about 1 object using another to do something.

This pattern has 3 parts in-total:

  • The object using a strategy (typically, a view controller in an iOS app)
  • The Strategy Protocol — This protocol defines methods that every strategy must implement
  • The strategies are objects that conform to the Strategy Protocol

When Should You Use It?

  1. Use the strategy pattern when you have 2 or more different interchangeable behaviors
  2. Unlike Delegation, the strategy pattern defines a family of objects
  3. Strategies are intended to be easily changeable at runtime whereas Delegation are often fixed at runtime

Code

AnimeViewController.swift

AnimeDetailStrategy is the Strategy Protocol, and I defined the properties and methods that any Strategy must implement when conforming to the protocol. AnimeViewController is a view controller containing a table view & table view cell from the Object Library in Interface Builder that is populated with animeArray (an array containing 2 Strategies as elements).

It’s important to note that I set the anime or Strategy property in AnimeDetailViewController with the specific Strategy that the user decides to click in the table view delegate method — didSelectRowAt.

AnimeDetailViewController.swift

I set the default view of the AnimeDetailViewController to AnimeDetailView using the Identity Inspector in Interface Builder, and use animeDetailView (property that references to the view) to populate the view using the properties & methods from the anime or Strategy property.

AttackOnTitansStrategy.swift

SwordArtOnlineStrategy.swift

Conforming to the AnimeDetailStrategy protocol, both of the anime Strategies share the same properties and methods except with different values.

AnimeView.swift

AnimeDetailView.swift

Building on top of the Model View Controller design pattern, I keep the AnimeView (view of the AnimeViewController) and AnimeDetailView (view of the AnimeDetailViewController) in their own modules inside a View directory.

Similarly with AnimeDetailViewController, I set the class behind the default view of the AnimeViewController to be AnimeView using the Identity Inspector.

Main.storyboard

I embedded the AnimeViewController within a navigation controller, and please make sure to set Auto Layout constraints to make sure you are seeing the UI elements properly when deploying your application.

Finished Project

What Should You Be Careful About?

  • Be careful about pre-optimizing and overusing this pattern.
  • If a behavior won’t ever change, it’s perfectly okay to code it in a controller / object.

Conclusion

In case if you haven’t yet, join my Discord server! I’m looking to create a community of developers so we can reinforce, motivate, and get to know each other in our programming journey.

Discord

If you have any questions or comments, please don’t feel afraid to ask or connect with me on social media! You can also send me an email at jkim@cloudiosx.com

LinkedIn

AngelList

GitHub

Portfolio

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store