Improving Your E-Commerce Shipping Module Efficiency: A Guide to Dynamic Class Instantiation using the Strategy Pattern in PHP
Published: 2023-03-21 16:18:19

In e-commerce platforms, shipping is an important part of the overall business strategies. The shipping module of an e-commerce platform needs to be designed in a way that allows for flexibility in shipping methods and pricing. This is where the strategy pattern can be used. In this blog post, we'll explore the strategy pattern in PHP using the shipping module of an e-commerce platform as an example.

Let's first try to understand what problems strategy design pattern solves.

**The Strategy Pattern**

The strategy pattern is a design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. This pattern lets the algorithm vary independently from the clients that use it. In other words, it allows you to interchange different algorithms without changing the code that uses them.

**Problem Example**

The shipping module of an e-commerce platform is a perfect use case for the strategy pattern. There are many different ways to handle shipping, and each method has its own set of rules and calculations. By using the strategy pattern, you can define each shipping method as a separate algorithm, and the e-commerce platform can easily switch between them without changing the code that handles the shipping.

**Implementing the Strategy Pattern in PHP**

To implement the strategy pattern in PHP, we'll need to create a few classes. First, we'll create an interface called ShippingStrategy. This interface will define the methods that all shipping strategies must implement.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7nb9mp6w97ujmzvju9k.png)

Next, we'll create a class for each shipping method that implements the ShippingStrategy interface. For example, we could create a class called StandardShipping that implements the ShippingStrategy interface and handles the logic for standard shipping.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5so8n6ngvm4mkq06k7yt.png)

We may have another class for DomesticShipping and InternationalShipping that implements the ShippingStrategy interface and handles the logic for standard shipping.

**DomesticShipping Class**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/da6brupn49lp3v0nrt0r.png)

**InternationalShipping Class**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8875myneo7o3kcil3wj.png)

Now, we will class a Shipping class that will calculate the shipping cost and estimated delivery date on the basis of the shipping type provided via its __construct() method.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly46h28jrdzd43qglrpe.png)

In above image, the "Shipping" class represents a shipping service that calculates the shipping cost and estimates the delivery date based on the chosen shipping type. It uses the strategy pattern to dynamically select the appropriate shipping strategy based on the given shipping type, and delegates the calculation of the shipping cost and estimated delivery date to the selected strategy object.

Let's see how to use this class.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iif9gebylg56n85l14o9.png)

Here, we are passing $shippingType that can be of any type/strategy that we have implemented. 

Here, you can find the github repo link to understand it in better way.

[Gihub Repo](https://github.com/asifzcpe/desing-patterns/tree/main/strategy-pattern)