Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

State Pattern

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

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

structures

The State Pattern contains the following main roles:

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


More articles