1.状态模式,当我们处理一些情况是,要依据别人对象的状态来决定处理的内容,我们呢可以 为不同的状态生成不同的类,在核心类中,我们就可以依据不同的状态来得到不同的处理。
2.一个较为复杂的示例:
package com.cn;
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;
import java.util.HashSet;
interface Context {
public abstract void setClock(int hour);
public abstract void changeState(State state);
public abstract void callSecurityCenter(String msg);
public abstract void recordLog(String msg);
}
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);
}
class DayState implements State {
private static DayState singleton = new DayState();
private DayState(){
}
public static State getInstance() {
return singleton;
}
public void doClock(Context context, int hour) {
if (hour < 9 || 17 <= hour) {
context.changeState(NightState.getInstance());
}
}
public void doUse(Context context) {
context.recordLog("使用金库(白天)");
}
public void doAlarm(Context context) {
context.recordLog("警铃(白天)");
}
public void doPhone(Context context) {
context.callSecurityCenter("一般的通话(白天)");
}
public String toString() {
return "【白天】";
}
}
class NightState implements State {
private static NightState singleton = new NightState();
private NightState(){
}
public static State getInstance() {
return singleton;
}
public void doClock(Context context, int hour) {
if (hour >= 9 &&17 > hour) {
context.changeState(DayState.getInstance());
}
}
public void doUse(Context context) {
context.recordLog("异常:晚间使用金库!");
}
public void doAlarm(Context context) {
context.recordLog("警铃(晚间)");
}
public void doPhone(Context context) {
context.callSecurityCenter("晚间的通话录音");
}
public String toString() {
return "【晚间】";
}
}
class SafeFrame extends Frame implements ActionListener ,Context {
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 panel = new Panel();
panel.add(buttonUse);
panel.add(buttonAlarm);
panel.add(buttonPhone);
panel.add(buttonExit);
add(panel, BorderLayout.SOUTH);
pack();
setVisible(true);
buttonUse.addActionListener(this);
buttonAlarm.addActionListener(this);
buttonPhone.addActionListener(this);
buttonExit.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("" + e);
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("?");
}
}
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);
}
public void changeState(State state) {
System.out.println("状态已经从" + this.state + "变成" + state + "。");
this.state = state;
}
public void callSecurityCenter(String msg) {
textScreen.append("call!" + msg + "\n");
}
public void recordLog(String msg) {
textScreen.append("record..." + msg + "\n");
}
}
public class Main {
public static void main(String[] args) {
SafeFrame frame = new SafeFrame("State Sample");
HashSet<Integer> set = new HashSet<Integer>();
set.add(4);
System.out.println(set.size());
while (true) {
for (int hour = 0; hour < 24; hour ++) {
frame.setClock(hour);
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
}