JAVA设计模式之状态模式

状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。

例子:实现一个machine,有红绿蓝三种状态,有last和next两个按钮,按下按钮时会切换状态。

  1 public interface State
  2 {    
  3     public void last();
  4     public void next();
  5     public String getState();
  6 }
  7 
  8 public class RedState implements State
  9 {
 10     Machine machine;
 11     
 12     public RedState(Machine machine)
 13     {
 14         this.machine = machine;
 15     }
 16 
 17     public void last() 
 18     {
 19         machine.setState(new BlueState(machine));
 20     }
 21 
 22     public void next() 
 23     {
 24         machine.setState(new GreenState(machine));
 25     }
 26 
 27     public String getState() 
 28     {
 29         return "Red";
 30     }
 31 }
 32 
 33 public class GreenState implements State
 34 {
 35     Machine machine;
 36     
 37     public GreenState(Machine machine)
 38     {
 39         this.machine = machine;
 40     }
 41 
 42     public void last() 
 43     {
 44         machine.setState(new RedState(machine));
 45     }
 46 
 47     public void next() 
 48     {
 49         machine.setState(new BlueState(machine));
 50     }
 51 
 52     public String getState() 
 53     {
 54         return "Green";
 55     }
 56 }
 57 
 58 public class BlueState implements State
 59 {
 60     Machine machine;
 61     
 62     public BlueState(Machine machine)
 63     {
 64         this.machine = machine;
 65     }
 66 
 67     public void last() 
 68     {
 69         machine.setState(new GreenState(machine));
 70     }
 71 
 72     public void next() 
 73     {
 74         machine.setState(new RedState(machine));
 75     }
 76 
 77     public String getState() 
 78     {
 79         return "Blue";
 80     }
 81 }
 82 
 83 public class Machine 
 84 {
 85     private State state;
 86 
 87     public String getState()
 88     {
 89         return state.getState();
 90     }
 91 
 92     public void setState(State state) 
 93     {
 94         this.state = state;
 95     }
 96     
 97     public void last()
 98     {
 99         state.last();
100     }
101     
102     public void next()
103     {
104         state.next();
105     }
106 }
107 
108 public class Test 
109 {
110     private static final int interval = 500; 
111     
112     public static void main(String[] args) throws InterruptedException  
113     {
114         Machine machine = new Machine();
115         machine.setState(new RedState(machine));
116         System.out.println("last: ");
117         for(int i = 0; i < 3; i++)
118         {
119             System.out.println("state:"+machine.getState());
120             machine.last();
121             Thread.sleep(interval);
122         }      
123         machine.setState(new RedState(machine));
124         System.out.println("\nnext: ");
125         for(int i = 0; i < 3; i++)
126         {
127             System.out.println("state:"+machine.getState());
128             machine.next();
129             Thread.sleep(interval);
130         }         
131     }
132 }

 

posted @ 2016-06-16 15:37  hxy_has_been_used  阅读(219)  评论(0编辑  收藏  举报