Home >Backend Development >PHP Tutorial >Learn PHP design patterns PHP implements state pattern_php skills
1. Intention
Allows an object to change its behavior when its internal state changes. The object appears to have modified its class
The position where the state mode changes is the state of the object
2. State pattern structure diagram
3. Main characters in status mode
Abstract state (State) role: Define an interface to encapsulate the behavior corresponding to a specific state of the environment object
ConcreteState role: Each concrete state class implements the behavior corresponding to a state of the environment (Context)
Context role: Defines the interface that the client is interested in and retains an instance of a specific state class. An instance of this concrete state class gives the current state of this environment object
4. Advantages and disadvantages of status mode
1. It localizes behaviors related to specific states
2. It makes the state transition visible
3. State objects can be shared
5. Applicable Scenarios of Status Mode
1. The behavior of an object depends on its state, and it must change its behavior according to the state at runtime
2. An operation contains a large number of conditional statements with multiple branches, and these branches depend on the state of the object. This state is usually represented by one or more enumeration constants. Often, there are multiple operations containing this same conditional structure. State mode puts each conditional branch into a separate class. This allows you to treat the state of the object as an object, which can change independently without relying on other objects
6. Status mode and other modes
Singleton mode: The specific state object is usually a singleton mode
Flyweight pattern (flyweight pattern): Flyweight pattern explains when and how to share state objects
7. State Mode PHP Example
<?php /** * 抽象状态角色 */ interface State { /** * 方法示例 */ public function handle(Context $context); } /** * 具体状态角色A * 单例类 */ class ConcreteStateA implements State { /* 唯一的实例 */ private static $_instance = null; private function __construct() { } /** * 静态工厂方法,返还此类的唯一实例 */ public static function getInstance() { if (is_null(self::$_instance)) { self::$_instance = new ConcreteStateA(); } return self::$_instance; } public function handle(Context $context) { echo 'Concrete Sate A handle method<br />'; $context->setState(ConcreteStateB::getInstance()); } } /** * 具体状态角色B * 单例类 */ class ConcreteStateB implements State { /* 唯一的实例 */ private static $_instance = null; private function __construct() { } /** * 静态工厂方法,返还此类的唯一实例 */ public static function getInstance() { if (is_null(self::$_instance)) { self::$_instance = new ConcreteStateB(); } return self::$_instance; } public function handle(Context $context) { echo 'Concrete Sate B handle method<br />'; $context->setState(ConcreteStateA::getInstance()); } } /** * 环境角色 */ class Context { private $_state; /** * 默认为StateA */ public function __construct() { $this->_state = ConcreteStateA::getInstance(); } public function setState(State $state) { $this->_state = $state; } public function request() { $this->_state->handle($this); } } /** * 客户端 */ class Client { /** * Main program. */ public static function main() { $context = new Context(); $context->request(); $context->request(); $context->request(); $context->request(); } } Client::main(); ?>
The above is the code to implement the state mode using PHP. There are also some conceptual distinctions about the state mode. I hope it will be helpful to everyone's learning.