State mode (State)



 GOF Intent: To allow an object to change its behavior when its internal state changes. ( The same calling class has different reactions in different states )

 

Role:

Context class (Context): defines the interface that the client is interested in. Maintain an instance of the ConcreteState class, which is the state of the context.

Abstract state class (State): abstract state, there is a method handle () is the response of the current state of the context.

ConcreteState:  Concrete state class.

 

Implementation points:

The context is the main class, and the state is the different states of the context. Set the context.setState() t state, the user calls context.requese(), and the context directly calls the handle() method in the state. Achieving different reactions according to different states. Can be used in place of switch-case constructs.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802226&siteId=291194637