This article mainly introduces the status mode in the php design mode and uses php to implement the status mode. if you are interested, refer to it.
I. intention
Allows an object to change its behavior when its internal state changes. The object seems to have modified its class.
The State mode changes in the object state.
II. status mode structure
III. main roles in status mode
Abstract State role: defines an interface to encapsulate behaviors corresponding to a specific State of an environmental object.
ConcreteState role: each state class implements a 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 status class. The instance of this specific status class shows the existing status of the environment object
IV. Advantages and disadvantages of status mode
1. it will localize the behavior related to a specific state
2. it makes the status transition display
3. the State object can be shared.
V. applicable scenarios of the status mode
1. the behavior of an object depends on its state, and it must change its behavior according to its state at runtime.
2. an operation contains a large number of multi-branch condition statements, and these branches depend on the state of the object. This state is usually represented by one or more enumerated constants. Generally, multiple operations contain the same condition structure. In State mode, each condition branch is placed in an independent class. This allows you to take the state of the object as an object in the case of the object itself. this object can be independent of other objects.
VI. status mode and other modes
Singleton mode: The State object is usually singleton mode.
The flyweight mode explains when and how to share state objects.
VII. status mode PHP example
<? Php/*** abstract State role */interface State {/*** method example */public function handle (Context $ context );} /*** specific status role A * singleton class */class ConcreteStateA implements State {/* unique instance */private static $ _ instance = null; private function _ construct () {}/*** static factory method, return the unique instance */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
'; $ Context-> setState (ConcreteStateB: getInstance ());}} /*** specific status role B * singleton class */class ConcreteStateB implements State {/* unique instance */private static $ _ instance = null; private function _ construct () {}/*** static factory method, return the unique instance */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
'; $ Context-> setState (ConcreteStateA: getInstance () ;}/ *** environment role */class Context {private $ _ state; /*** default value: StateA */public function _ construct () {$ this-> _ state = ConcreteStateA: getInstance ();} public function setState (State $ state) {$ this-> _ state = $ state;} public function request () {$ this-> _ state-> handle ($ this );}} /*** Client */class Client {/*** Main program. */public static func Tion main () {$ context = new Context (); $ context-> request (); $ context-> request () ;}} Client: main () ;?>
The above is the code that uses php to implement the State mode. there are some concepts about the state mode, and I hope it will be helpful for you to learn.