PHP design mode-state mode
The State mode allows an object to change its behavior when its internal state changes. This object seems to have changed its class. The State mode mainly solves the problem when the conditional expressions that control the state of an object are too complex. Transferring the status judgment logic to a series of classes that indicate different states can simplify the complicated judgment logic.
UML class diagram:
Role:
Context Environment (Work): it defines the interface required by the customer program, maintains an instance of a specific State role, and delegates status-related operations to the current specific object for processing.
Abstract State: defines an interface to encapsulate a specific State-related behavior that uses the context environment.
Specific state (AmState): an interface that implements the definition of abstract state.
Core code:
* User: Jang * Date: 2015/6/10 * Time: * // Status interface IState {function WriteCode (Work $ w );} // class AmState implements IState {public function WriteCode (Work $ w) {if ($ w-> hour <= 12) {echo current time: {$ w-> hour}, working in the morning; sleepy, lunch break .;} Else {$ w-> SetState (new PmState (); $ w-> WriteCode ();}}} // class PmState implements IState {public function WriteCode (Work $ w) {if ($ w-> hour <= 17) {echo current time: {$ w-> hour}, working in the afternoon is good, continue to work hard .;} Else {$ w-> SetState (new NightState (); $ w-> WriteCode ();}}} // evening Work status class NightState implements IState {public function WriteCode (Work $ w) {if ($ w-> IsDone) {$ w-> SetState (new BreakState ()); $ w-> WriteCode ();} else {if ($ w-> hour <= 21) {echo current time: {$ w-> hour}. work overtime, exhausted .;} Else {$ w-> SetState (new SleepState (); $ w-> WriteCode ();}}}} // rest status class BreakState implements IState {public function WriteCode (Work $ w) {echo current time: {$ w-> hour}. It's home from Work .;} // Sleep status class SleepState implements IState {public function WriteCode (Work $ w) {echo current time: {$ w-> hour}, no problem, falling asleep .;} // Working status class Work {private $ current; public function Work () {$ this-> current = new AmState ();} public $ hour; public $ isDone; public function SetState (IState $ s) {$ this-> current = $ s;} public function WriteCode () {$ this-> current-> WriteCode ($ this );}}
Call the client test code:
// ------------------------- Status mode --------------------------- require_once. /State. php; $ emergWork = new Work (); $ emergWork-> hour = 9; $ emergWork-> WriteCode (); $ emergWork-> hour = 10; $ emergWork-> WriteCode (); $ emergWork-> hour = 13; $ emergWork-> WriteCode (); $ emergWork-> hour = 14; $ emergWork-> WriteCode (); $ emergWork-> hour = 17; $ emergWork-> WriteCode (); $ emergWork-> IsDone = true; $ emergWork-> IsDone = false; $ emergWork-> hour = 19; $ emergWork-> WriteCode (); $ emergWork-> hour = 22; $ emergWork-> WriteCode ();
Applicable scenarios:
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 multi-branch structure, and these branches depend on the object state.
Advantages
1. The status mode localized the behavior associated with a specific State and separated the behavior of different States.
2. All status-related code exists in a ConcereteState, so it is easy to add new States and transformations by defining new subclasses.
3. The State mode reduces dependencies between different State transfer logics by dividing them into sub-classes of State.
Disadvantages
Resulting in a large number of ConcreteState subclasses
Welcome to my video course. The address is as follows. Thank you.
PHP Object-Oriented Design Model