Red > Green > Refactor > Red

cycle is based on desire

状態を表現管理する State パターン

(有限オートマトン)

Singleton は未考慮

f:id:mat5ukawa:20150922215717p:plain

↓ classify

f:id:mat5ukawa:20150922220123p:plain

src
  |
  +-- jp.ymatsukawa
      +-- Main.java
      |
      +-- state
           |
           + -- Worker.java
           + -- WeekdayState.java
           + -- MorningState.java
           + -- AfternoonState.java
           + -- EveningState.java
           + -- NightState.java

jp.ymatsukawa/Main.java

package jp.ymatsukawa;

import jp.ymatsukawa.state.Worker;

public class Main {
  public static void main(String[] args) {
    Worker worker = new Worker(); // First state is MorningState

    for(int aDay = 0; aDay < 4; ++aDay) {
      worker.showState();
      worker.transWeekdayState();
    }
    worker.showState(); // Final State is MorningState (Before State is NightState)
  }
}

System out

Physical energy is 100: ah it's morning, but sleepy.
Physical energy is 70: after lunch, it's sleepy.
Physical energy is 50: tiredness makes me sleepy.
Physical energy is 30: it's sleepy, good night.
Physical energy is 100: ah it's morning, but sleepy.

jp.ymatsukawa/state/Worker.java

package jp.ymatsukawa.state;

public class Worker {
  private WeekdayState weekdayState;
  public Worker() {
    this.weekdayState = new MorningState();
  }

  public void transWeekdayState() {
    Class weekdayState = this.weekdayState.getClass();
    if (weekdayState.equals(MorningState.class)) {
      this.weekdayState = new AfternoonState();
    } else if (weekdayState.equals(AfternoonState.class)) {
      this.weekdayState = new EveningState();
    } else if (weekdayState.equals(EveningState.class)) {
      this.weekdayState = new NightState();
    } else if (weekdayState.equals(NightState.class)) {
      this.weekdayState = new MorningState();
    }
  }

  public void showState() {
    System.out.print("Physical energy is " + String.valueOf(this.weekdayState.getPhysicalEnergy()) + ": ");
    System.out.println(this.weekdayState.getThought());
  }
}

jp.ymatsukawa/state/WeekdayState.java

package jp.ymatsukawa.state;

public interface WeekdayState {
  public int getPhysicalEnergy();
  public String getThought();
}

jp.ymatsukawa/state/MorningState.java

package jp.ymatsukawa.state;

public class MorningState implements WeekdayState {
  @Override
  public int getPhysicalEnergy() {
    return 100;
  }

  @Override
  public String getThought() {
    return "ah it's morning, but sleepy.";
  }
}

jp.ymatsukawa/state/AfternoonState.java

package jp.ymatsukawa.state;

public class AfternoonState implements WeekdayState {
  @Override
  public int getPhysicalEnergy() {
    return 70;
  }

  @Override
  public String getThought() {
    return "after lunch, it's sleepy.";
  }
}

jp.ymatsukawa/state/EveningState.java

package jp.ymatsukawa.state;

public class EveningState implements WeekdayState {
  @Override
  public int getPhysicalEnergy() {
    return 50;
  }

  @Override
  public String getThought() {
    return "tiredness makes me sleepy.";
  }
}

jp.ymatsukawa/state/NightState.java

package jp.ymatsukawa.state;

public class NightState implements WeekdayState {
  @Override
  public int getPhysicalEnergy() {
    return 30;
  }

  @Override
  public String getThought() {
    return "it's sleepy, good night.";
  }
}

technical reference from

github.com

thanks