Strategy pattern in Swift

Tram Ho

What is it?

In software development, if you know how to apply design patterns, you will quickly get simple but effective designs when maintaining, upgrading or extending them. One of the simple and easy-to-implement designs that I submit in this article is Strategy


Design patterns are classified into three categories according to their goals:


Yes, the Strategy pattern is a behavioral pattern because it is related to the behavior of objects. Easy, right?

When should we use it?

There are many cases when we can use it. Let’s talk about three very specific scenarios in which we can see a clear value of Strategy pattern.

Different ways to do the same
When you need to do the same thing in your code in different ways, it is a clear example that you can use it.
Instead of inheritance
If you need to extend the functionality of a class and to do this, you need to create a new class that inherits from it.
Alternative to if / else blocks
Sometimes, if you look at a class, you may find that it has too many if / else or switches, I mean, conditional blocks. This is an indication that this class has more responsibilities than necessary. Using a Strategy pattern will help you disperse them.

Real Example

Imagine we need to create a Logger class, who will print a message in the console. In addition, this class allows you to print stylized messages: lowercase, uppercase and uppercase. So a deployment might be something like this

After that, we will have an enum to specify which type is available and the function lo with switch inside to print the message in the control panel in different types.
We should modify this class by adding new types inside the enum and the log function

According to the scenarios we have seen before, this may be a good example in which the Strategy pattern can help us.

To explain how, we will answer the three questions and use the diagram I extracted from the Design Patterns by Tutorials book, available in the Ray Wenderlich store.

What : A protocol that defines the action we want to encapsulate. In our example, the action will be a log a message
Who: An object containing the object that matches the strategy. In our example, it could be an object using the strategy log the message
How: Specific strategy. Each implementation is different. In our example, we will have three strategies, one for each style.
So, the previous example using the strategy looks like this:

And here is how we can use it:

Why should we use Strategy

Strategy is one of the simplest and most useful designs. Also, if we use this model, you’re doing a lot of good things and you probably didn’t know:

Single responsibility: When we create strategies to do different things, we realize that our original class has fewer responsibilities.

Open / Close. : If we use Strategy to extend the functionality of objects we do not need to update the original object. As in our first example, if we want to add a new style, we just need to create a new Strategy. If not, we will need to add a new instance in our enum Styles.

Share the news now

Source : Viblo