search
HomeBackend DevelopmentPHP TutorialStrategy pattern: one of the design patterns
Strategy pattern: one of the design patternsAug 28, 2023 pm 05:53 PM
Design Patternsstrategy patternKey words

Strategy pattern: one of the design patterns

So far, we have covered three design patterns in this series. We define four different categories of design patterns. In this article, I will explain the Strategic Design Pattern, which falls under the Behavioral Design Patterns.

You may have a question: When should you use this design pattern? I would say when we have multiple methods (algorithms) to perform the same operation and we want the application to choose a specific method based on the parameters you have. This mode is also called strategy mode.

A very simple example for this article is the sorting function. For example, we have multiple algorithms for sorting arrays, but depending on the number of array elements, we should choose which algorithm to use to get the best performance.

This mode is also called strategy mode.

question

I will give an example of an e-commerce website that integrates multiple payment gateways. Although the website has multiple payment gateways, upon request, they are not all displayed on the front-end. Instead, the appropriate payment gateway needs to be selected on the fly based on the cart amount.

As a simple example, if the cart value is less than $500, the payment should be processed using PayPal standards, but if the amount is $500 or more, it should be processed using the stored credit card details (assuming the details have been storage).

Without the correct strategy implemented, our code would look like this:

First we will provide the main classes for payment via Paypal and payment via credit card, which will be added below.

// Class to pay using Credit Card
class payByCC {

    private $ccNum = '';
	private $ccType = '';
	private $cvvNum = '';
	private $ccExpMonth = '';
	private $ccExpYear = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using Credit Card";
	}
	
}

// Class to pay using PayPal
class payByPayPal {

	private $payPalEmail = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using PayPal";
	}

}

// This code needs to be repeated every place where ever needed.
$amount  = 5000;
if($amount >= 500) {
	$pay = new payByCC();
	$pay->pay($amount);
} else {
	$pay = new payByPayPal();
	$pay->pay($amount);
}

Here, you might say that we need to put conditional statements to make our code work properly. Imagine how many changes you need to make when we need to make new changes to this logic or you find a bug in this logic. We have to add patches to all places where this code is used.

solution

We will implement the same requirement, but using the Strategy pattern, which allows us to make our code clearer, easier to understand and extensible.

interface

First, we will implement the interfaces that all the different payment gateway classes will use. Ultimately, these are our strategies.

interface payStrategy {
    public function pay($amount);
}

class payByCC implements payStrategy {
	
	
	private $ccNum = '';
	private $ccType = '';
	private $cvvNum = '';
	private $ccExpMonth = '';
	private $ccExpYear = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using Credit Card";
	}
	
}

class payByPayPal implements payStrategy {

	private $payPalEmail = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using PayPal";
	}

}

Next we will create our main class, which can use different strategies than the ones we have implemented so far.

class shoppingCart {
    	
	public $amount = 0;
	
	public function __construct($amount = 0) {
		$this->amount = $amount;
	}
	
	public function getAmount() {
		return $this->amount;
	}
	
	public function setAmount($amount = 0) {
		$this->amount = $amount;
	}

	public function payAmount() {
		if($this->amount >= 500) {
			$payment = new payByCC();
		} else {
			$payment = new payByPayPal();
		}
		
		$payment->pay($this->amount);
		
	}
}

Here we can see that the conditional loading of our payment method is done in the payAmount method. Let's wrap everything together and see how to use it further.

interface payStrategy {
    public function pay($amount);
}

class payByCC implements payStrategy {
	
	private $ccNum = '';
	private $ccType = '';
	private $cvvNum = '';
	private $ccExpMonth = '';
	private $ccExpYear = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using Credit Card";
	}
	
}

class payByPayPal implements payStrategy {

	private $payPalEmail = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using PayPal";
	}

}

class shoppingCart {
	
	public $amount = 0;
	
	public function __construct($amount = 0) {
		$this->amount = $amount;
	}
	
	public function getAmount() {
		return $this->amount;
	}
	
	public function setAmount($amount = 0) {
		$this->amount = $amount;
	}

	public function payAmount() {
		if($this->amount >= 500) {
			$payment = new payByCC();
		} else {
			$payment = new payByPayPal();
		}
		
		$payment->pay($this->amount);
	}
}

$cart = new shoppingCart(499);
$cart->payAmount();

// Output
Paying 499 using PayPal

$cart = new shoppingCart(501);
$cart->payAmount();

//Output 
Paying 501 using Credit Card

We can see that the payment gateway rollover is not transparent to the application. Depending on the parameters, it has the appropriate payment gateway available to process the transaction.

Add new strategy

If at a later stage the user needs to add a new strategy with different logic (here a new payment gateway), it will be very simple in this case. Let’s say we want to add a new payment gateway, Moneybooker, and want to process funds when the cart amount exceeds $500 but falls below $1,000.

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

class payByMB implements payStrategy {

    private $mbEmail = '';
	
	public function pay($amount = 0) {
		echo "Paying ". $amount. " using Money Booker";
	}

}

We now have our new strategy class ready, all we need to change is the main payAmount method. Need to be modified as follows:

public function payAmount() {
    
	if($this->amount > 500 && $this->amount < 1000) {
		$payment = new payByMB();
	} else if($this->amount >= 500) {
		$payment = new payByCC();
	} else {
		$payment = new payByPayPal();
	}
	
	$payment->pay($this->amount);
}

Here you can see that we only made the change in the payAmount method, not in the client code that calls the method.

in conclusion

So to conclude, when we have multiple ways to perform the same task (in software languages, when we have multiple algorithms to perform the same operation), we should consider implementing the Strategy pattern.

By using this mode, we can add/remove algorithms freely since the switching of these algorithms is not transparent to the application.

I've tried my best to provide a basic but useful example to demonstrate the Strategy Design Pattern, but if you have additional comments or questions, please feel free to add them to the feed below.

The above is the detailed content of Strategy pattern: one of the design patterns. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在PHP后端功能开发中合理应用设计模式?如何在PHP后端功能开发中合理应用设计模式?Aug 07, 2023 am 10:34 AM

如何在PHP后端功能开发中合理应用设计模式?设计模式是一种经过实践证明的解决特定问题的方案模板,可以用于构建可复用的代码,在开发过程中提高可维护性和可扩展性。在PHP后端功能开发中,合理应用设计模式可以帮助我们更好地组织和管理代码,提高代码质量和开发效率。本文将介绍常用的设计模式,并给出相应的PHP代码示例。单例模式(Singleton)单例模式适用于需要保

如何通过编写代码来学习和运用 PHP8 的设计模式如何通过编写代码来学习和运用 PHP8 的设计模式Sep 12, 2023 pm 02:42 PM

如何通过编写代码来学习和运用PHP8的设计模式设计模式是软件开发中常用的解决问题的方法论,它可以提高代码的可扩展性、可维护性和重用性。而PHP8作为最新版的PHP语言,也引入了许多新特性和改进,提供更多的工具和功能来支持设计模式的实现。本文将介绍一些常见的设计模式,并通过编写代码来演示在PHP8中如何运用这些设计模式。让我们开始吧!一、单例模式(Sing

深入聊聊设计模式利器之“职责链模式”(附go实现流程)深入聊聊设计模式利器之“职责链模式”(附go实现流程)Jan 17, 2023 am 11:43 AM

本篇文章给大家带来了关于golang设计模式的相关知识,其中主要介绍了职责链模式是什么及其作用价值,还有职责链Go代码的具体实现方法,下面一起来看一下,希望对需要的朋友有所帮助。

Go语言中的ETL的设计模式Go语言中的ETL的设计模式Jun 01, 2023 pm 09:01 PM

随着数据的增长和复杂性的不断提升,ETL(Extract、Transform、Load)已成为数据处理中的重要环节。而Go语言作为一门高效、轻量的编程语言,越来越受到人们的热捧。本文将介绍Go语言中常用的ETL设计模式,以帮助读者更好地进行数据处理。一、Extractor设计模式Extractor是指从源数据中提取数据的组件,常见的有文件读取、数据库读取、A

深入解析Go语言中的单例模式深入解析Go语言中的单例模式Mar 21, 2023 pm 06:36 PM

单例模式是一种常见的设计模式,它在系统中仅允许创建一个实例来控制对某些资源的访问。在 Go 语言中,实现单例模式有多种方式,本篇文章将带你深入掌握 Go 语言中的单例模式实现。

设计模式的六大原则是什么设计模式的六大原则是什么Jan 06, 2023 pm 04:25 PM

设计模式的六大原则:1、单一职责原则,其核心就是控制类的粒度大小、将对象解耦、提高其内聚性;2、开闭原则,可以通过“抽象约束、封装变化”来实现;3、里氏替换原则,主要阐述了有关继承的一些原则;4、依赖倒置原则,降低了客户与实现模块之间的耦合;5、接口隔离原则,是为了约束接口、降低类对接口的依赖性;6、迪米特法则,要求限制软件实体之间通信的宽度和深度。

了解JavaScript中的设计模式和最佳实践了解JavaScript中的设计模式和最佳实践Nov 03, 2023 am 08:58 AM

随着JavaScript的不断发展和应用范围的扩大,越来越多的开发人员开始意识到设计模式和最佳实践的重要性。设计模式是一种被证明在某些情况下有用的软件设计解决方案。而最佳实践则是指在编程过程中,我们可以应用的一些最佳的规范和方法。在本文中,我们将探讨JavaScript中的设计模式和最佳实践,并提供一些具体的代码示例。让我们开始吧!一、JavaScript中

C#开发建议:设计模式与架构选择C#开发建议:设计模式与架构选择Nov 22, 2023 pm 03:53 PM

在C#开发中,设计模式和架构选择是至关重要的。良好的设计模式和合适的架构选择可以大大提高软件的可维护性、扩展性和性能。本文将讨论一些在C#开发中常用的设计模式和架构选择,并给出一些建议。设计模式是解决特定问题的通用解决方案,它们可以帮助开发人员避免重复造轮子,提高代码的可重用性和可读性。在C#开发中,有许多常用的设计模式,如单例模式、工厂模式、观察者模式等。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools