Overview
Have you ever entered to an e-commerce and buy a product?, If you have, then you may have been show different ways to pay (PayPal, Credit Card, etc.)
This purchase process is a way to see the Strategy Pattern. Strategy is a Behavioral design pattern that lets you define different strategies and switch between them to reach a goal. In the above image, you have a goal (buy a product) and different strategies to accomplish it (pay with PayPal, Credit Card, etc.).
Let’s see how we would implement this purchase process.
Structure of Strategy Pattern
First, let's understand the structure of the pattern. It is defined by 3 parts:
-
Context Class
This is our goal
-
Contract Interface
These are the rules to be followed by the Strategies
-
Strategy Classes
Refers to the different strategies to reach my goal, are the implementation of the
Contract Interface
Here is a Class Diagram for the Strategy structure
Will also need a Client who trigger the purchase process. Will see this in the code examples.
Code time, yay!
Now, let’s take the Class Diagram into code to see how it is done. I’ll be using Golang because it is the language I’m comfortable with:
Implement the Strategies
For this, will have 3 different classes (PayPal, CreditCard and Bank)
-
Client
In this example, I’ll be using the console to execute the purchase process.
When we run our program, this is what happen:
ℹ️ Note: This pattern is a way to implement the Open/Closed Principle of SOLID.
What if we want to add another payment method?
This would be easy, you just have to create another class that implements the PaymentMethodStrategy
interface:
and after that, you just have to register it in your context class
The conventional way
How would it be to implement this process without the Strategy pattern?
Well, instead of having 3 classes (Context, Contract and Strategies), we’ll only have 1 class that implements all the logic to pay with PayPal, CreditCard, Bank, etc.
- This will be our Purchase Class that implements all the logic
- Our client
When we run our program, this is what happen:
This could be fine when you first build your app, or when you know that you’ll rarely will add or modify existent logic. But if you know that the business logic will grow, things like this may happen in the future:
- It will be difficult to navigate throughout the code
- Highly chance you modify something you’re not supposed to
- Git conflicts when different people working on the same class
- A lot of conditionals to switch between the different payment methods
From the business perspective it may be ok because it works, but from the technical perspective is not because it will not be maintainable in the future.
ℹ️ Note: Putting all the logic in one class, for this example, will go against the Single Responsibility Principle of SOLID.
Practice
Here I showed what the Strategy Pattern is, how it is structured, and when you should use this pattern instead of putting all the logic in one class.
I invite you to implement this pattern to apply a discount to an order and share the code in the comments. Hope this blog helped you understand the Strategy pattern. See you in the next Design Pattern article of this series.
References
Dive into Design Patterns: This article is made by the notes I took from this book
Peek: Tool to record my terminal
Ray.so: For the code images
Article repository: You can find the examples in my GitHub
Top comments (3)
Nice explanation. Thanks.
Thanks, reyes. Excelent explaination and example.
Can I know what theme you currently use to show the snipe of code ?