设计模式3——State设计模式

来源:互联网 发布:sql如何删除表 编辑:程序博客网 时间:2024/04/29 04:57

State状态设计模式类似于Switch多路分支功能的开关,State状态模式机制如下:

状态模式UML图如下:


State状态设计模式用于改变对象的行为,在代理的生命周期里,随着状态变化从一个目标实现程序切换到另一个目标实现程序。

我们经常遇到如下的程序代码:

[java] view plaincopy
  1. public class Creature{  
  2.      private Boolean isFrog = true;//标识  
  3.      public void greet(){  
  4.            if(isForg){  
  5.         System.out.println(“Ribbet!”);  
  6.        }else{  
  7.         System.out.println(“Darling!”);  
  8.        }  
  9.      }  
  10.      //转换标识  
  11.      public void kiss(){  
  12.     isForg = false;   
  13.      }  
  14.      public static void main(String[] args){  
  15.            Creature creature = new Creature();  
  16.            creature.greet();  
  17.            creature.kiss();  
  18.            creature.greet();  
  19.      }  
  20. }  
上面例子代码中greet()方法在执行具体操作之前必须要判断一下标识,代码显得笨拙繁琐,使用简单State状态模式改写上面代码如下:

[java] view plaincopy
  1. public class Creature{  
  2.     //状态接口  
  3.     private interface State{  
  4.           String response();  
  5.     }  
  6.     private class Forg implements State{  
  7.           public String response(){  
  8.        return “Ribbet!”;  
  9.           }  
  10.     }  
  11.     private class Prince implements State{  
  12.           public String response(){  
  13.        return “Darling!”;  
  14.           }  
  15.     }  
  16.     private State state = new Forg();  
  17.     public void greet(){  
  18.           System.out.println(state.response);  
  19.     }  
  20.     public void kiss(){  
  21.           state = new Prince();  
  22.     }  
  23.     public static void main(String[] args){  
  24.           Creature creature = new Creature();  
  25.           creature.greet();  
  26.           creature.kiss();  
  27.           creature.greet();  
  28.     }   
  29. }  

State状态设计模式中,状态自动切换并传播,不需要再改动标识,代码显得非常优雅。

State状态设计模式一个基本框架如下:

[java] view plaincopy
  1. //状态接口  
  2. interface State{  
  3.     void operation1();  
  4.     void operation2();  
  5.     void operation3();  
  6. }  
  7. //状态实现类1  
  8. class implementation1 implements State{  
  9.     public void operation1(){  
  10.         System.out.println(“Implementation1.operation1()”);  
  11.     }  
  12.     public void operation2(){  
  13.         System.out.println(“Implementation1.operation2()”);  
  14.     }  
  15.    public void operation3(){  
  16.         System.out.println(“Implementation1.operation3()”);  
  17.     }  
  18. }  
  19. //状态实现类2  
  20. class implementation2 implements State{  
  21.     public void operation1(){  
  22.         System.out.println(“Implementation2.operation1()”);  
  23.     }  
  24.     public void operation2(){  
  25.         System.out.println(“Implementation2.operation2()”);  
  26.     }  
  27.    public void operation3(){  
  28.         System.out.println(“Implementation2.operation3()”);  
  29.     }  
  30. }  
  31. //服务提供者  
  32. class ServiceProvider{  
  33.     private State state;  
  34.     public ServiceProvider(State state){  
  35.          this.state = state;  
  36.     }  
  37.     //状态更改  
  38.     public void changeState(State newState){  
  39.          state = newState;  
  40.     }  
  41.     public void service1(){  
  42.           //……  
  43.           state.operation1();  
  44.           //……  
  45.           state.operation3();  
  46.     }  
  47.     public void service2(){  
  48.           //……  
  49.           state.operation1();  
  50.           //……  
  51.           state.operation2();  
  52.     }  
  53.     public void service3(){  
  54.           //……  
  55.           state.operation3();  
  56.           //……  
  57.           state.operation2();  
  58.     }  
  59. }  
  60. public class StateDemo{  
  61.     private ServiceProvider sp = new ServiceProvider(new Implementation1());  
  62.     private void run(ServiceProvider sp){  
  63.          sp.service1();  
  64.          sp.service2();  
  65.          sp.service3();  
  66.     }  
  67.     public static void main(String[] args){  
  68.         StateDemo demo = new StateDemo();  
  69.         demo.run(sp);  
  70.         sp.changeState(new Implementation2());  
  71.         demo.run(sp);  
  72.     }  
  73. }  

State状态模式和Proxy代理模式都为客户端程序提供了一个目标程序代理,真正的目标程序被代理所隐藏,当客户端程序调用目标程序时,首先将调用请求发送给代理,代理才真正调用目标程序,但是Proxy代理模式和State状态模式有如下区别:

(1).Proxy代理模式中被调用的目标程序只有一个,而State状态模式中被调用的目标程序有多个。

(2).Proxy代理模式的目的是控制客户端对目标程序的访问,而State状态模式是为了根据条件动态改变目标程序。

0 0