1 /**
   2  * StatePattern example
   3  */
   4 
   5 import java.awt.*;
   6 import java.awt.event.*;
   7 
   8 interface State {
   9     public abstract void doClock(Context context, int hour);
  10     public abstract void doUse(Context context);
  11     public abstract void doAlarm(Context context);
  12     public abstract void doPhone(Context context);
  13 }
  14 
  15 class DayState implements State {
  16     private static DayState singleton = new DayState();
  17     private DayState() {
  18     }
  19     public static State getInstance() {   // SingletonPattern
  20         return singleton;
  21     }
  22     public void doClock(Context context, int hour) { // time setting
  23         if(hour < 9 || 17 <= hour) {
  24             context.changeState(NightState.getInstance());
  25         }
  26     }
  27     public void doUse(Context context) {
  28         context.recordLog("safe using (day)");
  29     }
  30     public void doAlarm(Context context) {
  31         context.callSecurityCenter("alarm (day)");
  32     }
  33     public void doPhone(Context context) {
  34         context.callSecurityCenter("phone (day)");
  35     }
  36     public String toString() {
  37         return "[day]";
  38     }
  39 }
  40 
  41 class NightState implements State {
  42     private static NightState singleton = new NightState();
  43     private NightState() {
  44     }
  45     public static State getInstance() {
  46         return singleton;
  47     }
  48     public void doClock(Context context, int hour) {
  49         if(9 <= hour && hour < 17) {
  50             context.changeState(DayState.getInstance());
  51         }
  52     }
  53     public void doUse(Context context) {
  54         context.callSecurityCenter("emergency... Night safe use");
  55     }
  56     public void doAlarm(Context context) {
  57         context.callSecurityCenter("emergency bell... (Night)");
  58     }
  59     public void doPhone(Context context) {
  60         context.recordLog("recording night phone");
  61     }
  62     public String toString() {
  63         return "[night]";
  64     }
  65 }
  66 
  67 interface Context {
  68     public abstract void setClock(int hour);
  69     public abstract void changeState(State state);
  70     public abstract void callSecurityCenter(String msg);
  71     public abstract void recordLog(String msg);
  72 }
  73 
  74 class SafeFrame extends Frame implements ActionListener, Context {
  75     private TextField textClock = new TextField(60);
  76     private TextArea textScreen = new TextArea(10, 60);
  77     private Button buttonUse = new Button("Using safe");
  78     private Button buttonAlarm = new Button("Alarm");
  79     private Button buttonPhone = new Button("Phone");
  80     private Button buttonExit = new Button("Exit");
  81 
  82     private State state = DayState.getInstance();
  83 
  84     public SafeFrame(String title) {
  85         super(title);
  86         setBackground(Color.lightGray);
  87         setLayout(new BorderLayout());
  88 
  89         add(textClock, BorderLayout.NORTH);
  90         textClock.setEditable(false);
  91         add(textScreen, BorderLayout.CENTER);
  92         textScreen.setEditable(false);
  93 
  94         Panel panel = new Panel();
  95         panel.add(buttonUse);
  96         panel.add(buttonAlarm);
  97         panel.add(buttonPhone);
  98         panel.add(buttonExit);
  99         add(panel, BorderLayout.SOUTH);
 100         pack();
 101         show();
 102 
 103         buttonUse.addActionListener(this);
 104         buttonAlarm.addActionListener(this);
 105         buttonPhone.addActionListener(this);
 106         buttonExit.addActionListener(this);
 107     }
 108 
 109     public void actionPerformed(ActionEvent e) {
 110         System.out.println(""+e);
 111         if(e.getSource() == buttonUse) {
 112             state.doUse(this);
 113         } else if(e.getSource() == buttonAlarm) {
 114             state.doAlarm(this);
 115         } else if(e.getSource() == buttonPhone) {
 116             state.doPhone(this);
 117         } else if(e.getSource() == buttonExit) {
 118             System.exit(0);
 119         } else {
 120             System.out.println("?");
 121         }
 122     }
 123 
 124     public void setClock(int hour) {
 125         String clockstring = "current time is ";
 126         if(hour < 10) {
 127             clockstring += "0"+hour+":00";
 128         } else {
 129             clockstring += hour + ":00";
 130         }
 131         System.out.println(clockstring);
 132         textClock.setText(clockstring);
 133         state.doClock(this, hour);
 134     }
 135     public void changeState(State state) {
 136         System.out.println("from "+this.state+" to "+state);
 137         this.state = state;
 138     }
 139     public void callSecurityCenter(String msg) {
 140         textScreen.append("call! "+msg+"\n");
 141     }
 142     public void recordLog(String msg) {
 143         textScreen.append("record... "+msg+"\n");
 144     }
 145 }
 146 
 147 public class StateSafe {
 148     public static void main(String[] args) {
 149         SafeFrame frame = new SafeFrame("State Sample");
 150         while(true) {
 151             for(int hour=0; hour < 24; hour++) {
 152                 frame.setClock(hour);
 153                 try {
 154                     Thread.sleep(1000);
 155                 } catch(InterruptedException e) {
 156                 }
 157             }
 158         }
 159     }
 160 }
web biohackers.net