PHP design pattern strategy pattern
Introduction
The strategy pattern defines a family of algorithms and encapsulates them separately so that they can be replaced with each other. This model allows the algorithm to change independently of the customers using it.
Composed of
Abstract strategy role: Strategy class, usually implemented by an interface or abstract class.
Specific strategy role: packaging related algorithms and behaviors.
Environment role: holds a reference to a strategy class, which is ultimately called by the client.
Application scenarios
Multiple classes are only distinguished by different behaviors. You can use the strategy mode to dynamically select the specific behavior to be executed at runtime.
Different strategies (algorithms) need to be used in different situations, or the strategy may be implemented in other ways in the future.
The implementation details of specific strategies (algorithms) are hidden from customers and are completely independent of each other.
Implementation
Steps
Define the abstract role class (define the common abstract method of each implementation)
Define the specific strategy class (concretely implement the common method of the parent class)
Define the environment role class (receive the saved instance and execute it uniformly Strategy class interface method)
Code
<?php header('Content-Type:text/html;charset=utf-8'); /** * 策略模式演示代码 * * 为了更好地突出“策略”,我们这里以出行为例演示,日常出行大概分为以下几种工具:自驾车,公交车,地铁,火车,飞机,轮船 * * 下面一起看代码,领会何为策略模式 */ /** * Interface Travel 抽象策略角色 * 约定具体方法 */ interface Travel { public function go(); } /** * Class selfDriving 具体策略角色 * 自驾车 */ class bySelfDriving implements Travel { public function go() { echo '我自己开着车出去玩<br>'; } } /** * Class byBus具体策略角色 * 乘公交 */ class byBus implements Travel { public function go() { echo '我乘公交出去玩<br>'; } } /** * Class byMetro 具体策略角色 * 乘地铁 */ class byMetro implements Travel { public function go() { echo '我乘地铁出去玩<br>'; } } /** * Class byTrain 具体策略角色 * 乘火车 */ class byTrain implements Travel { public function go() { echo '我乘火车出去玩<br>'; } } /** * Class byAirplane 具体策略角色 * 乘飞机 */ class byAirplane implements Travel { public function go() { echo '我乘飞机出去玩<br>'; } } /** * Class bySteamship 具体策略角色 * 乘轮船 */ class bySteamship implements Travel { public function go() { echo '我乘轮船出去玩<br>'; } } /** * Class Mine 环境角色 */ class Mine{ private $_strategy; private $_isChange = false; /** * 构造方法 * 此处使用到了依赖注入和类型约束的概念,详情请参考 * 1.聊一聊PHP的依赖注入(DI) 和 控制反转(IoC) * @link https://segmentfault.com/a/1190000007209266 * 2.浅谈PHP的类型约束 * @link https://segmentfault.com/a/1190000007226476 * * @param Travel $travel */ public function __construct(Travel $travel) { $this->_strategy = $travel; } /** * 改变出行方式 * * @param Travel $travel */ public function change(Travel $travel) { $this->_strategy = $travel; $this->_isChange = true; } public function goTravel() { if ($this->_isChange) { echo '现在改变主意,'; $this->_strategy->go(); } else { $this->_strategy->go(); } } } /** * 客户端使用 */ $strategy = new Mine(new byBus()); // 乘公交 $strategy->goTravel(); // 乘地铁 $strategy->change(new byMetro()); $strategy->goTravel(); // 自驾车 $strategy->change(new bySelfDriving()); $strategy->goTravel(); // 其他根据具体应用选择实现
Running results
I took the bus to go out to play
Now I changed my mind, I took the subway to go out to play
Now I changed my mind, I drove out to play by myself
Advantages and disadvantages
Advantages
The strategy pattern provides a way to manage related algorithm families. A hierarchy of policy classes defines a family of algorithms or behaviors. Proper use of inheritance can transfer common code to the parent class, thereby avoiding duplication of code.
The strategy pattern provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If you are not using the strategy pattern, then the environment class that uses the algorithm or behavior may have some subclasses, each subclass providing a different algorithm or behavior. However, in this way the users of the algorithm or behavior are conflated with the algorithm or behavior itself. The logic that determines which algorithm to use or which behavior to take becomes mixed with the logic of the algorithm or behavior, making it impossible for it to evolve independently. Inheritance makes it impossible to dynamically change algorithms or behavior.
Using strategy pattern can avoid using multiple conditional transfer statements. Multiple transfer statements are not easy to maintain. It mixes the logic of which algorithm or behavior to adopt with the logic of the algorithm or behavior, and lists them all in one multiple transfer statement. It is more primitive and backward than the inheritance method. .
Disadvantages
The client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm class at the right time. In other words, the strategy pattern is only suitable if the client knows all the algorithms or behaviors.
The strategy pattern creates many strategy classes, and each specific strategy class will generate a new class. Sometimes it is possible to design a policy class to be shareable by saving environment-dependent state to the client, so that policy class instances can be used by different clients. In other words, you can use the flyweight pattern to reduce the number of objects.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
