Implement Strategy Pattern in a Purchase Process E-commerce System | by Hernan Reyes | Jun, 2022

Perceive how Technique sample works

Have you ever ever entered an e-commerce web site to purchase a product? When you have, then you might have been seen alternative ways to pay (PayPal, Credit score Card, and so forth.)

Buy course of

This buy course of is a approach to see the Technique Sample.

Technique is a Behavioral design sample that allows you to outline totally different methods and swap between them to succeed in a aim. Within the above picture, you will have a aim (purchase a product) and totally different methods to perform it (pay with PayPal, Credit score Card, and so forth.).

Let’s see how we might implement this buy course of.

First, let’s perceive the construction of the sample. It’s outlined by 3 components:

  1. Context Class: That is our aim
  2. Contract Interface: These are the foundations to be adopted by the Methods
  3. Technique Courses: Refers back to the totally different methods to succeed in my aim, are the implementation of the Contract Interface

Here’s a Class Diagram for the Technique construction:

Can even want a Shopper who set off the acquisition course of. Will see this within the code examples.

Now, let’s take the Class Diagram into code to see how it’s accomplished. I’ll be utilizing Golang as a result of it’s the language I’m comfy with:

Outline the Contract Interface

PaymentMethodStrategy Interface

Outline the Context

Buy UseCase

Implement the Methods

For this, can have 3 totally different lessons (PayPal, CreditCard, and Financial institution)

PayPal technique
Credit score Card technique
Financial institution technique


On this instance, I’ll be utilizing the console to execute the acquisition course of.

Shopper utilization

Once we run our program, that is what occur:

Word: This sample is a approach to implement the Open/Closed Precept of SOLID.

What if we need to add one other cost methodology?

This could be straightforward, you simply should create one other class that implements the `PaymentMethodStrategy` interface:

Bitcoin technique

And after that, you simply should register it in your context class:

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

How wouldn’t it be to implement this course of with out the Technique sample?

Effectively, as an alternative of getting 3 lessons (Context, Contract, and Methods), we’ll solely have 1 class that implements all of the logic to pay with PayPal, CreditCard, Financial institution, and so forth.

This can be our Buy Class that implements all of the logic:

Basic implementation

Our shopper

Shopper utilizing basic implementation

Once we run our program, that is what occur:

This could possibly be nice whenever you first construct our app, or when you understand that you simply’ll not often add or modify present logic. But when you understand that the enterprise logic will develop, issues like this will likely occur sooner or later:

  1. It will likely be troublesome to navigate the code
  2. Extremely probability you modify one thing you’re not presupposed to
  3. Git conflicts when totally different folks engaged on the identical class
  4. Lots of conditionals to change between the totally different cost strategies

From the enterprise perspective it could be okay as a result of it really works, however from the technical perspective just isn’t as a result of it is not going to be maintainable sooner or later.

Word: Placing all of the logic in a single class, for this instance, will go in opposition to the Single Accountability Precept of SOLID.

Right here I confirmed what the Technique Sample is, how it’s structured, and when you need to use this sample as an alternative of placing all of the logic in a single class.

I invite you to implement this sample to use a reduction to order and share the code within the feedback. Hope this weblog helped you perceive the Technique sample. See you within the subsequent Design Sample article of this collection.

  1. Dive into Design Patterns: This text is made by the notes I took from this guide
  2. Peek: Instrument to report my terminal
  3. Figma Jam: To make the illustrations
  4. Article repository: You’ll find the examples in my GitHub
  5. For the screenshots

More Posts