Strategy Design Pattern & Problem it solves using Swift

Shivam Jaiswal
Mac O’Clock
Published in
3 min readMay 2, 2020

In Strategy pattern, a class behaviour or its algorithm can be changed at run time. This type of design pattern comes under behaviour pattern.

In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

Went over your head?? 🤯 Don’t worry let’s break it down with an example step by step.

Step 1: Understanding the problem.

Let say, you own a Knife Factory which manufactures knives using two components blade & handle and right now you manufacturing only one kind of knives using blade1 & handle1.

This is how it’s class implementation would look like.

Later on another you also start making two more kinds of knives using (blade2, handle2) & (blade3, handle3) respectively.

So far so good!! Right??. But there are two problems in this design we followed.

Problem#1:

Right now we manufacturing 3 kind of blades & 3 kind of handles i.e. we can manufacture nCr : C(3,1) * C(3,1) = 9 kinds of knives using different combinations of blades & handles. As result we need to make 9 different KnifeFactory classes to support the manufacturing.

To generalise it we can say that if we are producing n kind of blades and m kind of handles & if we decide to produce all possible combinations of knives then the total number of classes would be C(m,1) * C(n,1) = O(m * n).

Which we can obviously tell is pretty inefficient, time consuming and it’s hard to manage so may classes.

Problem#2:

Let say we want to manufacture 4th knife using (blade2, handle3)

here we can see that it uses the blade() implementation of KniveFactory2 and handle() implementation of KniveFactory3 just by copy pasting the code from respective classes. Which leads to writing redundant code and by no means we are able to reuse existing implementation. Which is bad coding practice.

Step 2: Enough with problems let’s come to the solution.

To explain how to do it, we will answer three questions and use a diagram, which I extracted from the book Design Patterns by Tutorials, available in Ray Wenderlich’s store, where we can differentiate all the participants very well.

  • What: A protocol that defines the action that we want to encapsulate. In our example, the action would be to produce blade & handle.
  • Who: An object who contains an object who conforms the strategy. In our example, it could be an object who using the strategy to produce Knives.
  • How: Specific implementations of the strategy. Each implementation is different. In our example, we would have six strategies, two for each style.

So, the previous example using strategy would look like this:

And here how we can use it:

Let say we want to produce a knife using blade3 & handle1

How is this efficient ?

  1. Here, the cost of creating classes depends upon number of strategies we want to implement. In our case we can say that if we are producing n kind of blades and m kind of handles then the cost of creating strategies is O(m + n)
  2. You are not repeating any implementation of handle() & blade(). Hence the code redundancy has been completely removed .

Breaking down the definition

In Strategy pattern, we create objects which represent various strategies [i.e. BladeStrategy & HandleStrategy] and a context object [i.e KnifeFactory] whose behavior varies as per its strategy object. The strategy object changes the executing algorithm [manufacture(); behaviour varies as per it’s strategy object] of the context object.

References

  1. https://medium.com/flawless-app-stories/strategy-pattern-in-swift-1462dbddd9fe
  2. https://www.youtube.com/watch?v=v9ejT8FO-7I
  3. https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm

--

--

Shivam Jaiswal
Mac O’Clock

Just another developer, trying to make a diffrence.