Fundamental Design Pattern: Strategy

Cracking the Strategy Design Pattern

Introduction

  • Strategy

Strategy

  • 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

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

--

--

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
John Kim

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com