Design Patterns in Swift: Strategy Pattern

Sam Stone
Sam Stone
Nov 3, 2016 · 3 min read

Welcome to a series of posts dedicated to learning about design patterns. Whilst a lot of the ideas are code-agnostic, we’re aiming to show you how to implement them in Swift (Swift 3.0 as of the time of writing). Each post is independent of each other and all code for the projects will be available here on Git.

The Strategy pattern allows you to change the behaviour of an algorithm at run time. Using interfaces, we are able to define a family of algorithms, encapsulate each one, and make them interchangeable, allowing us to select which algorithm to execute at run time.

An example of this would be a character in a game. Our character — the Good Guy — has 3 states: running, walking and standing. We wish to reuse the states with other objects (i.e, the Bad Guy character), so we want to encapsulate them, and be able to use the states with other objects, reducing the amount of code.

We can show how the Good Guy character would working using a UML diagram:

To start, create a new project in Xcode. We won’t be touching anything in UIKit so create a macOS Terminal project.

First off, we need to define our interface/protocol (one of the same thing, but Swift uses the word protocol). Our basic interface defines one method called enterState(). It will look like this:

We can build the Good Guy class. This class is set up to receive our state and execute the enterState() method in the class we pass to it. It will look like this:

Then we can build our state classes. We will be creating walking, running and standing states. The idea is that the Good Guy will use these traits, but that the Bad Guy will also be able to use them when the time comes by implementing the CharacterState interface. They are very basic and only run a print command to show that we have entered that state. Here are the implementations for our states:

Now, in your main class, we want to be able to pass through the state to your Good Guy using the setState function we built into it. It will look like so:

We pass through different states to our Good Guy class, and it will execute each different (albeit basic) algorithm.

Hit run, and your output will look like this:

Entering walking state
Entering standing state
Entering running state
Program ended with exit code: 0

Hurray! You’ve implemented the Strategy pattern.

Bonus — Bad Guy

So as described above, the Bad Guy can also utilise the functions of the Good Guy without having to repeat any code (one big benefit of the Strategy pattern).

Create a new class called BadGuy.swift. This class will look similar to Good Guy but you will be able to add specific Bad Guy functionality to it (like, kill or shoot). Bad Guy will look like this:

To show how this works, in our main class, we will create a Bad Guy object, and set the state, just like we did with Good Guy, so it will now look like this:

Run it, and you will get this result:

Entering walking state
Entering standing state
Entering running state
Entering walking state
Entering standing state
Entering running state
Program ended with exit code: 0

You now have a Bad Guy and a Good Guy sharing the same code, without having to repeat the code for each object. Nice! As always, you can find the completed code here.

Design Patterns in Swift

A series of tutorials devoted to explaining various software design patterns in Swift.

Sam Stone

Written by

Sam Stone

Mobile Developer and Hip-Hop fan.

Design Patterns in Swift

A series of tutorials devoted to explaining various software design patterns in Swift.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade