Fundamental Design Pattern: Strategy
Cracking the Strategy Design Pattern
Design Patterns that will be covered in this article:
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?
- Use the strategy pattern when you have 2 or more different interchangeable behaviors
- Unlike Delegation, the strategy pattern defines a family of objects
- Strategies are intended to be easily changeable at runtime whereas Delegation are often fixed at runtime
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.
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.
Conforming to the AnimeDetailStrategy protocol, both of the anime Strategies share the same properties and methods except with different values.
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.
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.
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.
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.
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 firstname.lastname@example.org