State Pattern
In the State Pattern, the behavior of a class changes based on its state. This type of design pattern belongs to the behavioral patterns.
In the State Pattern, we create objects representing various states and a context object whose behavior changes as the state object changes.
The State Pattern allows an object to change its behavior when its internal state changes, so that the object behaves differently in different states. By encapsulating each state into a separate class, you can avoid using a large number of conditional statements to implement state transitions.
Introduction
Intent
Allows an object to change its behavior when its internal state changes, making it appear as if its class has changed.
Main problem solved
- The State Pattern solves the problem that an object's behavior depends on its state, allowing the object to switch behavior when the state changes.
Use Cases
- When there are a large number of conditional statements in the code, and these conditional statements depend on the object's state.
Implementation
- Define the state interface: Declare one or more methods to encapsulate the behavior of concrete states.
- Create concrete state classes: Implement the state interface and implement specific behavior according to different states.
- Define the context class: Contains a reference to a state object and updates its behavior when the state changes.
Key code
- State interface: Declares behavior methods.
- Concrete state classes: Implements the state interface and encapsulates specific behavior.
- Context Class: Maintains a state object and provides methods to change its state.
Application Example
- Basketball player states: An athlete can have states such as normal, abnormal, and exceptional.
- Zeng Houyi bells: The bells act as the context, and different bells (states) have different playing effects.
Advantages
- Encapsulate state transition rules: Encapsulate state transition logic inside the state objects.
- Easy to extend: Adding new state classes does not affect existing code.
- Centralize state-related behavior: Centralize all behavior related to a specific state into one class.
- Simplify conditional statements: Avoid using a large number of conditional statements to switch behavior.
- State sharing: Allows multiple context objects to share the same state object.
Disadvantages
- Increase the number of classes and objects: Each state requires a concrete state class.
- Complex to implement: The pattern structure and implementation are relatively complex.
- Insufficient support for the Open-Closed Principle: Adding new states or modifying state behavior may require modifying existing code.
Usage recommendation
- Consider using the State Pattern when an object's behavior changes with its state.
- The State Pattern is suitable for replacing complex conditional or branch statements.
Notes
- The State Pattern is suitable for situations with a limited number of states (usually no more than 5).
- Use with caution to avoid making the system too complex.
structures
The State Pattern contains the following main roles:
-
Context (Context): Defines the interface that the client is interested in and maintains a reference to the current state object. The context can delegate state-related behavior handling through the state object.
-
State: Defines an interface for encapsulating the behavior associated with one state of the context.
-
Concrete State: Implements the state interface and is responsible for handling behavior associated with that state. A concrete state object usually maintains an internal reference to the context object in order to switch to different states under different conditions.
Create a class that implements
We will create a State interface as the bridge implementation and concrete classes that implement the State The concrete state class of the interface.Context is a class with a certain state.
StatePatternDemo, and our demo class uses the Context and state objects to demonstrate the behavior changes of Context when the state changes.
Step 1
Create an interface.
State.java
public
interface
State
{
public
void
doAction
(
Context
context
)
;
}
Step 2
Create concrete classes that implement the interface.
StartState.java
public
class
StartState
implements
State
{
public
void
doAction
(
Context
context
)
{
System
.
out
.
println
(
"
Player is in start state
"
)
;
context
.
setState
(
this
)
;
}
public
String
toString
(
)
{
return
"
Start State
"
;
}
}
StopState.java
public
class
StopState
implements
State
{
public
void
doAction
(
Context
context
)
{
System
.
out
.
println
(
"
Player is in stop state
"
)
;
context
.
setState
(
this
)
;
}
public
String
toString
(
)
{
return
"
Stop State
"
;
}
}
Step 3
creation Context class.
Context.java
public
class
Context
{
private
State
state
;
public
Context
(
)
{
state
=
null
;
}
public
void
setState
(
State
state
)
{
this
.
state
=
state
;
}
public
State
getState
(
)
{
return
state
;
}
}
Step 4
Use Context to see when the state State changes, the behavior changes.
StatePatternDemo.java
public
class
StatePatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Context
context
=
new
Context
(
)
;
StartState
startState
=
new
StartState
(
)
;
startState
.
doAction
(
context
)
;
System
.
out
.
println
(
context
.
getState
(
)
.
toString
(
)
)
;
StopState
stopState
=
new
StopState
(
)
;
stopState
.
doAction
(
context
)
;
System
.
out
.
println
(
context
.
getState
(
)
.
toString
(
)
)
;
}
}
Step 5
Run the program, output result:
Player is in start state
Start State
Player is in stop state
Stop State