Better Programming

Advice for programmers.

Follow publication

Implement Strategy Pattern in a Purchase Process E-commerce System

Hernan Reyes
Better Programming
Published in
4 min readJun 6, 2022

Overview

Have you ever entered an e-commerce website to buy a product? If you have, then you may have been seen different ways to pay (PayPal, Credit Card, etc.)

Purchase process

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:

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:

Define the Contract Interface

PaymentMethodStrategy Interface

Define the Context

Purchase UseCase

Implement the Strategies

For this, will have 3 different classes (PayPal, CreditCard, and Bank)

PayPal strategy
Credit Card strategy
Bank strategy

Client

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

Client usage

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:

Bitcoin strategy

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

purchase.RegisterStrategy("Bitcoin", NewBitcoin())

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:

Classic implementation

Our client

Client using classic implementation

When we run our program, this is what happen:

This could be fine when you first build our app, or when you know that you’ll rarely add or modify existing 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 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 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: To make the illustrations
  4. Article repository: You can find the examples in my GitHub
  5. Ray.so: For the screenshots

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Hernan Reyes
Hernan Reyes

Written by Hernan Reyes

Hello, I’m a Backend dev currently working remotely and sharing my knowledge through articles and twitch if you speak spanish https://www.twitch.tv/hernanreyes_

Responses (1)

Write a response