Understanding use cases of Interfaces and Abstract Classes
2019-12-25
Observer Design pattern
2019-12-26
Show all

Strategy Design Pattern

9 mins read

What Actually a Strategy is?

Let’s start with a formal definition then we’ll break it down.

The definition taken straight from the Gang of Four book says a strategy pattern is ‘‘a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly into code, code receives run-time instructions as to which in a family of algorithms to use’’. Well, there are a lot of things going on in these two lines of definition. Let’s break it down.

Breaking it Down

Before breaking it down. Let’s talk a little bit about what is an algorithm?

If you are a programmer. You probably know what it is. Any procedure for conducting a sequence of specified actions for solving a particular problem is an algorithm. Now I want you to forget about what the words and what they mean in the context of programming for a minute and try to think about what I am going to convey here instead. Your car’s engine is responsible for moving your car. The engine performs a certain sequence of operations so the car can move. The car doesn’t really care if it is a Honda’s engine or if it is a skyline’s engine, as long as a car has a working engine a car can move.

But what if you want to change your car’s engine with another engine? What if you’ve bought skyline’s engine and need to utilize your same car?

A skyline’s engine performs much differently than a honda’s engine. But all of these engines are similar-looking engines from the outside and all operate similarly. Yes, we all know a car’s engine is interchangeable. But you have to go and find mechanics for that which will cost you extra money and time. Which is the same as going to a code a changing it manually. You don’t want that! Instead, you need an easy way to change your cars engine in a plug and play manner. So what do you need to do?

This is where the strategy pattern came in. This pattern allows you to create multiple algorithms and use them interchangeably. First, we have the abstraction(interface) of the engine of a car, represented by Engine. Then we can take that abstraction and create the Honda engine, skyline engine, and any other engine, each with its own specific implementations.

Now we can create different engines that aren’t strictly coupled with a car. This pattern makes it so that instead of implementing a single algorithm directly into code, we can make any number of algorithms and choose which to pick up on the run-time. If you don’t know what run-time is, it is just another way of saying the time during the execution of your code.

A Practical Example

Now that we understand what is strategy pattern at a high level, let’s take what we learned and apply it to a software development domain. Suppose you are creating a web application that authenticates the user before they can use your application. Perhaps you want your application to register using a Normal Form.

You could define a Web application class that will use the normal Form to register users, and everything would be fine before you decided to introduce the concept of Google or Facebook signup in your application.

class WebApplication{    public function register(){        
$user = new User();
$user->name = form('name');
$user->email = form('email');
$user->save();
}}$app = new WebApplication();
$app->register();

In the above code, I am using a form helper function to take out form values to create a new user. Imagine that you wanted to be able to create different methods to register a user. Perhaps you want a Google signup and a Facebook signup. You can create an IF-Else statement to add more registration types. This is fine your code will work. But You end up creating multiple IF, else statements for each or your registration methods. Your code is tightly coupled. Without using a strategy pattern, you’d have to follow the same pattern of creating a bunch of IF, Else statements for each of registration types. This could get out of hand really fast — especially when you want to create many registration types.

Strategy Pattern Saving Our life

Any time you have multiple algorithms to perform a single task in your code, you can simplify that code by using this pattern. Instead of having multiple pieces of logic residing in a single place. You make your web application utilize a Registration interface that will have specific implementations of different types of registrations.

The web application wouldn’t need to know about the differences between implementations (Google signup, Facebook signup, or normal form signup).

Instead, it will utilize a generic Registration interface, and you could create Google, Facebook, and any number of implementations out of that interface. These implementations are also called strategies.

Now you can create any number of registration types, and use it as plug and play functionality because you have decoupled the algorithms in a separate class hierarchy.

Implementing In Code

The code i am going to write will be pseudo-code in PHP, how ever design patters are not language-specific you can use and implement design patterns in any language and technology.

First of taking look at how you can utilize Registration interface in your web application.

interface Registration{
public function register(User $user);
}class WebApplication{ private $type;
private $user; public function __construct(){
$this->$user = new User();
} // set the registration type
public function setRegistration(Registration $type){
$this->type = $type;
} public function register(){
$type->register($this->user);
}
}

Hopefully, by this point, it makes a little more sense, but let’s talk about what each part is in our example.

Your application is now able to utilize a generic registration interface. There is a setRegistration() method which is responsible for setting an implementation of Registration interface. The register method is pretty much just giving the control to the strategy to save the supplied user object. But your application can not do much for now. Let’s write some implementations of Registration interface, that way we can test your application.

class GoogleSignup implements Registration{
public function register(User $user){
// perform all the operations to fetch name, email from user.
$google = new Google_api(); $user->eamil = $google->getEmail();
$user->name = $google->getName();
$user->save();
}
}class FacebookSignup implements Registration{ public function register(User $user){
$facebook = new Facebook_api(); $user->eamil = $facebook->getEmail();
$user->name = $facebook->getName();
$user->save();

}
}

The GoogleSignup Strategy connects to google’s API and fetches the user’s name and email from google and saves it. The FacebookSignup does the same the only difference is it uses Facebook’s API.

However, Google and Facebook’s API doesn’t work this way. I mean how the heck google should know which users’ information he has to provide us? For the sake of learning let’s keep it simple. Finally, it’s time to see how your little application is working.

$app = new WebApplication();// Register using google
$app->setRegistration(new GoogleSignup());
$app->register();// Register using Facebook
$app->setRegistration(new FacebookSignup());
$app->register();

Here you go, see your application is working pretty well. It can now change the algorithm of registering a new user anytime and however the heck it wants.

Signs Of When to Use the Strategy pattern?

I would say use it whenever there is a chance, but how would you know there is a chance? Try to look at your problems from all the perspectives you can come up with. Looking at problems from different perspectives will help you improve your skill in software analysis and design. Okay now let me share few signs that i can help you to immediately understand you can use strategy here.

  • Whenever you have a doubt you might have to change your business logic here is the future. I will say go ahead and use strategy so you don’t have to modify your code in the future. Just create a new strategy, plug it in and enjoy.
  • Use it, when ever your application it so much engaged with users actions and you have to change algorithms frequently based on the users actions.
  • Sometimes your application’s business logic can be much more difficult and lengthy. In these situations, you can separate your logic from the application by encapsulation it in a separate strategy class

Conclusion

I hope you’ve fully understood the significance of the strategy pattern. While it doesn’t take much effort to fully gasp the gist of the strategy pattern. In this article, I’ve covered everything from why to how and where of a strategy design pattern. I started by talking about what is strategy, then I break it down by giving easy-to-understand and practical examples with diagrams. At last, I shared some signs to identify when you can use a strategy pattern. Design patterns are not optional anymore in 2018. There are a lot of design patterns. This is just one of them. If you want to stand out from the crowd of average coders, learning design patterns in depth is your way. One book I will say explains every aspect of design patterns in a dead-simple manner is the Head First Design patterns book. It makes the concepts easy to digest.

References:

https://medium.com/@iamqamarali/strategy-design-pattern-in-depth-32750ae0ad6
https://refactoring.guru/design-patterns/strategy/python/example

https://medium.com/@sawomirkowalski/design-patterns-strategy-2262cfaa2648

Amir Masoud Sefidian
Amir Masoud Sefidian
Machine Learning Engineer

Comments are closed.