Home >Backend Development >PHP Tutorial >PHP Design Patterns - Strategy Pattern_PHP Tutorial
Statement: The reference material for this series of blogs is "Dahua Design Pattern", written by Cheng Jie.
The strategy pattern defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable. The Strategy pattern allows an algorithm to change independently of the clients using it, i.e. it encapsulates changes in the algorithm.
Applicable scenarios:
1. Multiple classes differ only in their behavior. You can use Strategy mode to dynamically select the specific behavior to be executed at runtime.
2. Different strategies (algorithms) need to be used in different situations, or the strategy may be implemented in other ways in the future.
3. The implementation details of specific strategies (algorithms) are hidden from customers and are completely independent of each other.
4. The client must know all strategy classes and decide which strategy class to use. The strategy mode is only applicable when the client knows all algorithms or behaviors.
5. The strategy pattern creates many strategy classes, and each specific strategy class will generate a new class.
Sometimes you can use the Flyweight pattern to reduce the number of objects by saving environment-dependent state to the client.
UML class diagram:
Character Analysis:
Abstract strategy role (RotateItem): Strategy class, usually implemented by an interface or abstract class.
Specific strategy role (ItemX): Packages related algorithms and behaviors.
Environment role (ItemContext): holds a reference to a strategy class and is ultimately called by the client.
Detailed code implementation:
<!--?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/5/16 * Time: 21:46 */ /**抽象策略角色 * Interface RotateItem */ interface RotateItem { function inertiaRotate(); function unInertisRotate(); } /**具体策略角色——X产品 * Class XItem */ class XItem implements RotateItem { function inertiaRotate() { echo 我是X产品,我惯性旋转了。<br/-->; } function unInertisRotate() { echo 我是X产品,我非惯性旋转了。 ; } } /**具体策略角色——Y产品 * Class YItem */ class YItem implements RotateItem { function inertiaRotate() { echo 我是Y产品,我<span style="'color:">不能</span>惯性旋转。 ; } function unInertisRotate() { echo 我是Y产品,我非惯性旋转了。 ; } } /**具体策略角色——XY产品 * Class XYItem */ class XYItem implements RotateItem { function inertiaRotate() { echo 我是XY产品,我惯性旋转。 ; } function unInertisRotate() { echo 我是XY产品,我非惯性旋转了。 ; } } class contextStrategy { private $item; function getItem($item_name) { try { $class=new ReflectionClass($item_name); $this->item=$class->newInstance(); } catch(ReflectionException $e) { $this->item=; } } function inertiaRotate() { $this->item->inertiaRotate(); } function unInertisRotate() { $this->item->unInertisRotate(); } }
<!--?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/5/16 * Time: 21:46 */ header(Content-Type:text/html;charset=utf-8); require_once ./Strategy/Strategy.php; $strategy=new contextStrategy(); echo <span style='color: #ff0000;'-->X产品
1. The strategy pattern provides a way to manage related algorithm families.
The hierarchy of strategy classes defines an algorithm or behavioral family.
Appropriate use of inheritance can transfer common code to the parent class, thereby avoiding duplication of code.
2. The strategy pattern provides a way to replace the inheritance relationship.
Inheritance can handle multiple algorithms or behaviors.
If the strategy pattern is not used, the environment class that uses algorithms or behaviors 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.
3. Using strategy mode can avoid using multiple conditional transfer statements.
Multiple transfer statements are not easy to maintain. They mix the logic of which algorithm to adopt or which behavior to adopt with the logic of the algorithm or behavior, and list them all in one multiple transfer statement, which is easier than using inheritance. Primitive and backward.
Disadvantages:
1. 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.
2. The strategy mode creates many strategy classes, and each specific strategy class will generate a new class.
Sometimes the policy class can be designed to be shareable by saving environment-dependent state into 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.