added state pattern example
This commit is contained in:
parent
6acdcaffa7
commit
876ce99c77
34
src/learn/simple_state/Context.java
Normal file
34
src/learn/simple_state/Context.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class Context {
|
||||||
|
private State onState;
|
||||||
|
private State offState;
|
||||||
|
|
||||||
|
private State currentState;
|
||||||
|
|
||||||
|
public Context() {
|
||||||
|
onState = new OnState(this);
|
||||||
|
offState = new OffState(this);
|
||||||
|
currentState = offState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentState(State currentState) {
|
||||||
|
this.currentState = currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getOffState() {
|
||||||
|
return offState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getOnState() {
|
||||||
|
return onState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void on() {
|
||||||
|
currentState.on();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void off() {
|
||||||
|
currentState.off();
|
||||||
|
}
|
||||||
|
}
|
12
src/learn/simple_state/Main.java
Normal file
12
src/learn/simple_state/Main.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Context context = new Context();
|
||||||
|
context.off();
|
||||||
|
context.off();
|
||||||
|
context.on();
|
||||||
|
context.on();
|
||||||
|
context.off();
|
||||||
|
}
|
||||||
|
}
|
20
src/learn/simple_state/OffState.java
Normal file
20
src/learn/simple_state/OffState.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class OffState implements State {
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
public OffState(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on() {
|
||||||
|
context.setCurrentState(context.getOnState());
|
||||||
|
System.out.println("Transition to on");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void off() {
|
||||||
|
System.out.println("Already off");
|
||||||
|
}
|
||||||
|
}
|
20
src/learn/simple_state/OnState.java
Normal file
20
src/learn/simple_state/OnState.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class OnState implements State {
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
public OnState(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on() {
|
||||||
|
System.out.println("Already on");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void off() {
|
||||||
|
context.setCurrentState(context.getOffState());
|
||||||
|
System.out.println("Transition to off");
|
||||||
|
}
|
||||||
|
}
|
6
src/learn/simple_state/State.java
Normal file
6
src/learn/simple_state/State.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public interface State {
|
||||||
|
void on();
|
||||||
|
void off();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user