State Pattern

State: internal state object defines current behavior. The state changes, the behavior changes. Thay vì dùng if else bên ngoài, ta dùng state bên trong.

public static void main(String[] args) {
Birth birth = new Birth();


}

public class Birth {

private State state;    // interface

public Birth() {
state = new HappyState(this);
}

/**
* Makes time pass for the mammoth
*/
public void timePasses() {
if (state.getClass().equals(HappyState.class)) {
changeStateTo(new AngryState(this));
} else {
changeStateTo(new HappyState(this));
}
}

private void changeStateTo(State newState) {
this.state = newState;
this.state.onEnterState();
}

@Override
public String toString() {
return "The Birth";
}

public void felling() {
this.state.observe();
}
}

public interface State {

void onEnterState();
void observe();

}

public class HappyState implements State {

private Birth birth;

public HappyState(Birth birth) {
this.birth = birth;
}

@Override
public void observe() {
System.out.println(String.format("%s is calm and peaceful.", birth));
}

@Override
public void onEnterState() {
System.out.println(String.format("%s calms down.", birth));
}

}

public class AngryState implements State {

private Birth birth;

public AngryState(Birth birth) {
this.birth = birth;
}

@Override
public void observe() {
System.out.println(String.format("%s is furious!", birth));
}

@Override
public void onEnterState() {
System.out.println(String.format("%s gets angry!", birth));
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s