Quantcast
Channel: Envato Tuts+ Code
Viewing all articles
Browse latest Browse all 5163

Design Patterns: The Strategy Pattern

$
0
0

Earlier have covered three design patterns in this series. We have defined four categories of different design patterns. In this article, I am going to explain about Strategy Design Pattern, which falls under Behavioral Design Patterns.

You might have a question, when we should use this design pattern? I would say, when we have some several ways (algorithms) to perform the same operation and we want the application pick the specific way based on parameters you have. This pattern is also known as a policy pattern.

Very basic and simple example for this article can be sorting feature. We have multiple algorithm to sort an array, but based on the amount of array element who should wisely choose the sorting algorithm.

This pattern is also known as a policy pattern.

The Problem

I will take an example of eCommerce website having multiple payment gateways integrated. Site has multiple payment gateways, but as per requirements it won't show up in the front end but appropriate payment gateway needs to be selected on the fly based on cart amount.

If we take a simple example then, if cart value is less than or $500 the it should process using Paypal standard but if it is $500 or more then it should process using stored Credit Card details (assuming details are already stored).

Without implementing proper strategy our code would look like below:

At first, we will have main classes for both, pay via Paypal and pay via credit card, which are added below.

Here you can say that we need to place conditional statements to have our code works. Imagine the amount of changes you need to perform when we require new changes in that logic or you have caught bug in that logic. We will have to add a patch to all those places where we have used that code.

The Solution

We will implement the same requirement but with the use of the Strategy pattern which enables us to make our code more clear, understandable and extendable. 

The Interface

First we will implement our interface which will be implemented by all our different payment gateway classes which are ultimately our strategies. 

Next we will create our main class which can use different strategy we have implemented so far.

Here we can see that our conditional loading of payment methods is done in payAmount method. Let's wrap all things together and see how we can use this further.

We can see that flipping of payment gateway is not transparent to the application. Based on parameter, it has proper payment gateway available to process the transaction.

Adding a new Strategy

When later stage if a user needed to add new strategy (new payment gateway here) with some different logic then it would be very simple in this case. Let's say we want to add new payment gateway moneybooker and want to process money when cart amount is more than $500 but less than $1000.

All we need to do is create a new strategy class which implements our interface and we are good to go.

We have our new strategy class ready now what we need to change is our main payAmount method. It needs to be changed as per below code:

Here you can see that we have made changes in our payAmount method only instead of client code from where this method invokes.

Conclusion

Hence to conclude, when we have multiple ways to perform the same task, in software language when we have multiple algorithms to perform the same operation, we should consider implementing the Strategy pattern. 

By using this pattern, we are free to add/remove an algorithm because switching of these algorithms is not transparent to the application. 

I have tried my best to provide an elementary and yet useful example to demonstrate the strategy design pattern, but if you have additional comments or questions, just don't hesitate to add them in the feed below.


Viewing all articles
Browse latest Browse all 5163

Trending Articles