読者です 読者をやめる 読者になる 読者になる

Stateパターン

引き出すのっ。その条件を

% find . -name "*.java" -print0 |xargs -0 cat                                []
public interface Context {
	void setClock(int hour);

	void recording(String msg);

	void callSecurityCenter(String msg);

	void changeState(State state);

}
public class DayState implements State {
	private static DayState singleton = new DayState();

	private DayState() {
	}

	public static State getInstance() {
		return singleton;
	}

	@Override
	public void doClock(Context context, int hour) {
		if (hour < 9 || 17 <= hour) {
			context.changeState(NightState.getInstance());
		}
	}

	@Override
	public void doUse(Context context) {
		context.recording("金庫使用(昼間)");
	}

	@Override
	public void doAlarm(Context context) {
		context.callSecurityCenter("非常ベル(昼間)");
	}

	@Override
	public void doPhone(Context context) {
		context.callSecurityCenter("通常の通話(昼間)");
	}

	@Override
	public String toString() {
		return "[昼間]";
	}

}
public class Main {

	public static void main(String[] args) {
		SafeFrame frame = new SafeFrame("State Sample");
		while (true) {
			for (int hour = 0; hour < 24; hour++) {
				frame.setClock(hour);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {

				}
			}
		}
	}

}
public class NightState implements State {
	private static State singleton = new NightState();

	private NightState() {
	}

	@Override
	public void doClock(Context context, int hour) {
		if (hour <= hour && hour < 17) {
			context.changeState(DayState.getInstance());
		}
	}

	@Override
	public void doUse(Context context) {
		context.callSecurityCenter("非常:夜間の金庫使用");
	}

	@Override
	public void doAlarm(Context context) {
		context.callSecurityCenter("非常ベル(夜間)");
	}

	@Override
	public void doPhone(Context context) {
		context.recording("夜間の通話録音");
	}

	public static State getInstance() {
		return singleton;
	}

	@Override
	public String toString() {
		return "[夜間]";
	}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SafeFrame extends Frame implements Context, ActionListener {
	private static final long serialVersionUID = 1L;
	private TextField textClock = new TextField(60);
	private TextArea textScreen = new TextArea(10, 60);
	private Button buttonUse = new Button("金庫使用");
	private Button buttonAlarm = new Button("非常ベル");
	private Button buttonPhone = new Button("通常通話");
	private Button buttonExit = new Button("終了");

	private State state = DayState.getInstance();

	public SafeFrame(String title) {
		super(title);
		setBackground(Color.lightGray);
		setLayout(new BorderLayout());
		add(textClock, BorderLayout.NORTH);
		textClock.setEditable(false);

		add(textScreen, BorderLayout.CENTER);
		textScreen.setEditable(false);

		Panel panel1 = new Panel();
		panel1.add(buttonUse);
		panel1.add(buttonAlarm);
		panel1.add(buttonPhone);
		panel1.add(buttonExit);
		add(panel1, BorderLayout.SOUTH);

		pack();
		setVisible(true);
		buttonUse.addActionListener(this);
		buttonAlarm.addActionListener(this);
		buttonPhone.addActionListener(this);
		buttonExit.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.toString());
		if (e.getSource() == buttonUse) {
			state.doUse(this);
		} else if (e.getSource() == buttonAlarm) {
			state.doAlarm(this);
		} else if (e.getSource() == buttonPhone) {
			state.doPhone(this);
		} else if (e.getSource() == buttonExit) {
			System.exit(0);
		} else {
			System.out.println("?");
		}
	}

	@Override
	public void setClock(int hour) {
		String clockString = "現在時刻は";
		if (hour < 10) {
			clockString += "0" + hour + ":00";
		} else {
			clockString += "" + hour + ":00";
		}
		System.out.println(clockString);
		textClock.setText(clockString);
		state.doClock(this, hour);
	}

	@Override
	public void recording(String msg) {
		textScreen.append("record.. " + msg + "\n");
	}

	@Override
	public void callSecurityCenter(String msg) {
		textScreen.append("call! " + msg + "\n");
	}

	@Override
	public void changeState(State state) {
		System.out.println(this.state + "から" + state + "へ状態が変化しました");
		this.state = state;
	}

}
public interface State {
	public abstract void doClock(Context context, int hour);

	public abstract void doUse(Context context);

	public abstract void doAlarm(Context context);

	public abstract void doPhone(Context context);
}