IT. Expert System.

Design patterns

State Pattern


In State pattern a class behavior changes based on its state. This type of design pattern comes under behavior pattern.

In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.

Implementation

We're going to create a State interface defining a action and concrete state classes implementing the State interface. Context is a class which carries a State.

StaePatternDemo, our demo class will use Context and state objects to demonstrate change in Context behavior based on type of state it is in.

state pattern uml diagram

Step 1

Create an interface.

Image.java

public interface State {
public void doAction(Context context);
}

Step 2

Create concrete classes implementing the same 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

Create 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 the Context to see change in behaviour when State 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

Verify the output.

Player is in start state
Start State
Player is in stop state
Stop State



Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 1366 / . Delta: 0.02765 с