Added state pattern sample
This commit is contained in:
21
state/src/main/java/com/iluwatar/AngryState.java
Normal file
21
state/src/main/java/com/iluwatar/AngryState.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class AngryState implements State {
|
||||
|
||||
private Mammoth mammoth;
|
||||
|
||||
public AngryState(Mammoth mammoth) {
|
||||
this.mammoth = mammoth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void observe() {
|
||||
System.out.println(String.format("%s is furious!", mammoth));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnterState() {
|
||||
System.out.println(String.format("%s gets angry!", mammoth));
|
||||
}
|
||||
|
||||
}
|
16
state/src/main/java/com/iluwatar/App.java
Normal file
16
state/src/main/java/com/iluwatar/App.java
Normal file
@ -0,0 +1,16 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
|
||||
Mammoth mammoth = new Mammoth();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
|
||||
}
|
||||
}
|
32
state/src/main/java/com/iluwatar/Mammoth.java
Normal file
32
state/src/main/java/com/iluwatar/Mammoth.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class Mammoth {
|
||||
|
||||
private State state;
|
||||
|
||||
public Mammoth() {
|
||||
state = new PeacefulState(this);
|
||||
}
|
||||
|
||||
public void timePasses() {
|
||||
if (state.getClass().equals(PeacefulState.class)) {
|
||||
changeStateTo(new AngryState(this));
|
||||
} else {
|
||||
changeStateTo(new PeacefulState(this));
|
||||
}
|
||||
}
|
||||
|
||||
private void changeStateTo(State newState) {
|
||||
this.state = newState;
|
||||
this.state.onEnterState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "The mammoth";
|
||||
}
|
||||
|
||||
public void observe() {
|
||||
this.state.observe();
|
||||
}
|
||||
}
|
21
state/src/main/java/com/iluwatar/PeacefulState.java
Normal file
21
state/src/main/java/com/iluwatar/PeacefulState.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class PeacefulState implements State {
|
||||
|
||||
private Mammoth mammoth;
|
||||
|
||||
public PeacefulState(Mammoth mammoth) {
|
||||
this.mammoth = mammoth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void observe() {
|
||||
System.out.println(String.format("%s is calm and peaceful.", mammoth));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnterState() {
|
||||
System.out.println(String.format("%s calms down.", mammoth));
|
||||
}
|
||||
|
||||
}
|
9
state/src/main/java/com/iluwatar/State.java
Normal file
9
state/src/main/java/com/iluwatar/State.java
Normal file
@ -0,0 +1,9 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public interface State {
|
||||
|
||||
void onEnterState();
|
||||
|
||||
void observe();
|
||||
|
||||
}
|
Reference in New Issue
Block a user