Strategy Pattern in Swift iOS

Tram Ho

Difficulty: Beginner | Easy | Normal | Challenging
Xcode 14.0.1
Swift 5.7


1. About

In this article, I will introduce to you a Fundamental Design Pattern – Strategy Pattern. So what is the Strategy Pattern like, its application and how to implement it?


In my opinion, learning Design Pattern is quite dry and difficult to understand. So I won’t talk about theory/definition right away. I will show an example of Strategy Pattern first, then we will dive into its essence!!!

2. Example of Strategy Pattern

  • First, I have a protocol called Strategy that has an execute function with 2 parameters a and b. What to do with these 2 parameters, the protocol Strategy does not care (Of course, because this protocol has already been abstracted)


  • In the next part, I will have 3 classes that conform / implement protocol Strategy :

=> Looking at the above code, we will also understand what the effect of the above 3 functions is, right everyone. Includes 3 descriptive functions: sum of 2 numbers a and b, difference of 2 numbers a and b, product of 2 numbers a and b.


  • And we have one last part, the Context class which contains the protocol Strategy . It is important to note that the Context class contains the protocol Strategy , not one of the three concrete classes ConcreteStrategy .


  • Now that we have all 3 components that make up the Strategy Pattern, I will show you how to use this pattern for everyone right here:

  • From the above example, we can have an idea of ​​this pattern, right? Same class Context , but when applied “tact” to add 2 numbers a and b ( ConcreteStrategyAdd ), when running executeStrategy function of Context class, we will get the result: a + b = 15
  • At the same time, also while running this Context class, if I apply a different “tact” to the class. For example, “tact” for the difference of two numbers a and b ( ConcreteStrategySubtract ), or “tact” for the product of two numbers a and b ( ConcreteStrategyMultiply ). We will get the same result as our example code.

3. What is Strategy Pattern?

  • The Strategy Pattern belongs to the Behavioral Design Pattern group. It will define multiple objects (which are interchangeable) in different classes. Simultaneously can be interchanged in the run time of the program.
  • The strategy pattern defines a family of interchangeable objects that can be set or switched at runtime – Design Pattern by Tutorials
  • Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable – Dive Into Design Pattern

Those are some definitions of the Strategy Pattern. If people still find it difficult to understand, I will briefly explain like this: Strategy_Diagram.png

  • Strategy Pattern it will consist of 3 parts (like the picture or example in part 2):
    • A protocol Strategy contains the functions we need to use (corresponding to the box ” <<Protocol>> Strategy Protocol “)
    • A Context class contains the above protocol Strategy (corresponding to the box ” Object using a Strategy
    • And classes that conform Strategy (these classes are called Concrete Strategy are classes with different behaviors/strategies). They need to conform Strategy to be able to pass the above Context class. And since they all conform to the protocol Strategy , they can be changed/interchanged at runtime

=> Note : the important keyword in Strategy Pattern is that these “strategies” ( Concrete Strategy ) can be changed in RUNTIME , everyone.

4. When to use Strategy Pattern?

  • Use the Strategy Pattern when you have 2 or more variants of the same object (those variants have different behaviors). And those variants are interchangeable at runtime.
  • Use the strategy pattern when you have two or more different behaviors that are interchangeable. – Design Patterns by Tutorials
  • Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime. – Dive Into Design Pattern

5. Pros and cons

5.1. Advantage

  • It is possible to change the algorithms/behavior in the object during runtime.
  • It is possible to separate the detailed implementation of the algorithm/behavior from the code that uses it (in Example 2 we separated the implementation – 3 classes of Concrete from the code that uses it – class Context )
  • When we used Strategy Pattern, we used composition instead of inheritance
  • True to the Open/Closed Principle (the O principle in SOLID). When we want to add an algorithm/behavior, we won’t need to modify the existing algorithm/behavior.

5.2. Defect

  • If we only have 2 or less algorithms/behaviors, and they don’t change at runtime. Then do not use Strategy Pattern, using Strategy Pattern in this case will complicate the problem unnecessarily.
  • Users/programmers must know + understand the difference between algorithms/behaviors in order to choose the right algorithm/behavior to use.
  • Creating more classes to create algorithms/behaviors is often unnecessary. Because in some languages ​​there is an anonymous function to help do this.

6. Conclusion

  • So we have learned what the Strategy Pattern is and how to implement it through this blog.
  • If you have any questions or have a way to optimize your Strategy Pattern example, don’t hesitate to comment. Thank you so much = Thank a lot = Thank you very much.

In addition, you can read some more examples of Strategy Pattern:

Reference

Share the news now

Source : Viblo