Added state pattern sample

This commit is contained in:
Ilkka Seppala
2014-08-23 08:47:44 +03:00
parent b3d48cc4df
commit 9166d47ffc
7 changed files with 123 additions and 0 deletions
@@ -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();
}
}