Photo by Timon Studler on Unsplash

Make objects interchangeable in a family of business logic

Swift — Problems Catalogue #21

Alex Ilovan
Published in
3 min readFeb 21, 2023

--

Problem Definition:

Consider the following scenario. You have to implement a game where you have different characters that can jump with different styles.

The game allows players to change the jumping styles of their character dynamically, depending on the current situation. We need a way to define different kinds of algorithms (one for every jump style) and interchange them at will.

Problem Solution:

Solution —Strategy it’s a behavioral design pattern that defines a family of algorithms, encapsulates each one of them and make them interchangeable. The pattern lets the algorithm vary independently from the clients that use it.

In essence, it defines a common interface for algorithms, so that the client code can work with any concrete strategy without knowing its class. The client code holds a reference to a strategy and uses it to execute the algorithm. The client can change the strategy dynamically at runtime, depending on the situation.

Real-World Usage:

First, we need to define aJumpStrategy protocol that has a single method, jump(), which is used to perform a specific jumping style.

The ShortJump and LongJump classes are concrete implementations of the JumpStrategy protocol, they define different jumping styles that can be performed. Each class has a single method jump() that will be called when the character wants to perform a jump and a message is printed to indicate the type of jump.

1. Jump Strategy

Next, we define a Character class that holds a reference to a jump strategy object, it is initialized with one of the jump strategy implementations and it has a method jump() which triggers the execution of the jump strategy.

In this way, the character can change the jump style dynamically, depending on the current situation.

2. Character

Finally, let’s bring everything together by creating a character with a short jump style and then calling the jump() method on the character, which causes the character to perform a short jump and the message "Short Jump" is printed.

Then we change the character's jump strategy to long jump, and again call the jump() method on the character, which causes the character to perform a long jump and the message "Long Jump" is printed.

3. Bringing everything together

The strategy pattern allows you to define a family of jumping styles, encapsulate each one of them and make them interchangeable. The character that uses the jump strategy doesn’t need to know the details of the jump, it only needs to know that it exists and what it does. This way the character can change the jump dynamically, depending on the current situation.

This pattern is useful when you have multiple jumping styles that need to be used in different situations and you want to avoid using if-else or switch-case statements to choose the right jumping style and also make it easy to add new jumping styles or change existing ones without affecting the character code.

From this point on, the sky is the limit 🚀 well…almost.

Of course, this design pattern has its limitations but used in moderation, it’s a great tool in our development toolbox.

This is the next article in the Swift Problems Catalogue series in which I’ll tackle general software development problems. The aim is to have a quick reference guide that can be easily accessed when having a design/algorithm dillemma.

Let me know what you think and don’t be shy to share where and when this pattern simplified your coding experience 🎶

--

--

Alex Ilovan

🚀Head of Mobile Development @S&P 💻Comp. Engineer 🪐Engineering Manager. You can visit at: https://www.linkedin.com/in/alex-ilovan-129161b4/