DEV Community

Cover image for How the Strategy Pattern works?
Hernan Reyes
Hernan Reyes

Posted on

How the Strategy Pattern works?

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.)

Strategy pattern description

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:

  1. Context Class

    This is our goal

  2. Contract Interface

    These are the rules to be followed by the Strategies

  3. 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

Strategy Pattern Class Diagram description

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:

  1. Define the Contract Interface
    Contract definiton

  2. Define the Context
    Purchase context implenmentation

  3. Implement the Strategies
    For this, will have 3 different classes (PayPal, CreditCard and Bank)
    PayPal strategy implementationCreditCard strategy implementationBank strategy implementation

  4. Client

    In this example, I’ll be using the console to execute the purchase process.
    Client

When we run our program, this is what happen:

Strategy terminal example

ℹ️ 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:Bitcoin strategy implementation

and after that, you just have to register it in your context class

Bitcoin registry

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.

  1. This will be our Purchase Class that implements all the logic Classic implementationClassic implementation
  2. Our client Classic implementation client

When we run our program, this is what happen:

Classic example

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:

  1. It will be difficult to navigate throughout the code
  2. Highly chance you modify something you’re not supposed to
  3. Git conflicts when different people working on the same class
  4. 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

  1. Dive into Design Patterns: This article is made by the notes I took from this book

  2. Peek: Tool to record my terminal

  3. Figma Jam and IconDuck: To make the illustrations

  4. Ray.so: For the code images

  5. Article repository: You can find the examples in my GitHub

Latest comments (3)

Collapse
 
whtbl3 profile image
Phúc Lương

Can I know what theme you currently use to show the snipe of code ?

Image description

Collapse
 
jamiepgood profile image
Jamie Good

Nice explanation. Thanks.

Collapse
 
hariprasetia profile image
Hari Prasetia

Thanks, reyes. Excelent explaination and example.