Skip to content

State java

The State Design Pattern

Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package edu.redwoods.state;

public class TVRemote {

    public static void main(String[] args) {
        // State client 1 completely decoupled from states.
        TVContext context1 = new TVContext(true);
        context1.doAction(); // State transition 1
        context1.doAction(); // State transition 2, polymorphic behavior!!!

        // State client 2 where the client controls the states and transitions!
        State tvStartState = new TVStartState();
        State tvStopState = new TVStopState();
        TVContext context2 = new TVContext();

        context2.setState(tvStartState);
        context2.doAction();
        context2.setState(tvStopState);
        context2.doAction();

        // State client 3 where client sets up transitions, but they are automatic
        TVContext context3 = new TVContext();
        State tvStartState2 = new TVStartState(context3);
        State tvStopState2 = new TVStopState(context3, tvStartState2);
        context3.setState(tvStopState2);
        tvStartState2.setTransition(tvStopState2);

        context3.doAction();
        context3.doAction();
    }
}

State Interface

1
2
3
4
5
6
7
8
package edu.redwoods.state;

public interface State {
    public void doAction();
    public void setTransition(State state);
    public void setContext(TVContext context);
    public void transition();
}

Concrete State

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package edu.redwoods.state;

public class TVStartState implements State {
    private TVContext context;
    private State transition;

    public TVStartState() {
        this(null, null);
    }

    public TVStartState(TVContext context) {
        this(context, null);
    }

    public TVStartState(TVContext context, State transition) {
        this.transition = transition;
        this.context = context;
    }

    @Override
    public void doAction() {
        System.out.println("TV is turned ON");
        transition();
    }

    @Override
    public void setTransition(State next) {
        this.transition = next;
    }

    @Override
    public void setContext(TVContext context) {
        this.context = context;
    }

    @Override
    public void transition() {
        if(this.context != null && this.transition != null) {
            this.context.setState(transition);
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package edu.redwoods.state;

public class TVStopState implements State {
    private TVContext context;
    private State transition;

    public TVStopState() {
        this(null, null);
    }

    public TVStopState(TVContext context) {
        this(context, null);
    }

    public TVStopState(TVContext context, State transition) {
        this.transition = transition;
        this.context = context;
    }

    @Override
    public void doAction() {
        System.out.println("TV is turned OFF");
        transition();
    }

    @Override
    public void setTransition(State next) {
        this.transition = next;
    }

    @Override
    public void setContext(TVContext context) {
        this.context = context;
    }

    @Override
    public void transition() {
        if(this.context != null && this.transition != null) {
            this.context.setState(transition);
        }
    }
}

Context

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package edu.redwoods.state;

public class TVContext {
    private State tvState;
    public TVContext(boolean fullService) {
        State tvStartState = new TVStartState(this);
        State tvStopState = new TVStopState(this, tvStartState);
        tvStartState.setTransition(tvStopState);
        this.tvState = tvStopState;
    }
    public TVContext() {}

    public TVContext(State initial) {
        this.tvState = initial;
    }

    public void setState(State state) {
        this.tvState=state;
    }

    public State getState() {
        return this.tvState;
    }

    public void doAction() {
        this.tvState.doAction();
    }
}