Files
java-design-patterns/state/src/main/java/com/iluwatar/Mammoth.java

38 lines
648 B
Java
Raw Normal View History

2014-08-23 08:47:44 +03:00
package com.iluwatar;
2014-08-31 11:20:02 +03:00
/**
*
* Mammoth has internal state that defines its behavior.
*
2014-08-31 11:20:02 +03:00
*/
2014-08-23 08:47:44 +03:00
public class Mammoth {
2014-08-23 08:47:44 +03:00
private State state;
public Mammoth() {
state = new PeacefulState(this);
}
2014-08-23 08:47:44 +03:00
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();
}
2014-08-23 08:47:44 +03:00
@Override
public String toString() {
return "The mammoth";
}
2014-08-23 08:47:44 +03:00
public void observe() {
this.state.observe();
}
}