Design Patterns state mode

State mode

   State mode to study the behavior of an object wrapped in a different state objects inside,

   Each object belongs to a state of a state of a subclass of abstract class.

   The intention is to make the state a model object changes in its internal state when their behavior has changed.

Defined state (stateing) mode:
similar strategy mode
subject in the state, the complex "decision logic" extract to a different state object,
allowing the state of the object to change its behavior when its internal state changes.

Environment (Context) Role :
  also called context, which defines the interface of interest to clients, maintaining a current state,
  and the state commission associated with the operation of the current state of the object to be processed.

Abstract state (stateing) Roles :
  defining an interface for a particular package environment state object corresponding to behavior.

Specific state (Concrete stateing) Role :
  The abstract states corresponding behavior.
  1 public class stateing {
  2     public static void main(String[] args) {
  3         context context = new context();
  4         context.setSta(context.getClose());
  5         context.close();
  6         context.open();
  7         context.run();
  8         context.close();
  9     }
 10 }
 11 
 12 //环境角色
 13 class context {
 14     //状态
 15     privateSTA State;
 16      // behavior 
. 17      Private Opening Open;
 18 is      Private closeing Close;
 . 19      Private Runing with RUN;
 20 is  
21 is      public  void setSta (State STA) {
 22 is          the this .STA = STA;
 23 is          // add environmental variables based state, to facilitate changes in the environment variables based state 
24          sta.setCon ( the this );
 25      }
 26 is  
27      public context () {
 28          the this .Open = new new Opening ();
 29          the this .close =new closeing();
 30         this.run = new runing();
 31     }
 32 
 33     public void open() {
 34         sta.open();
 35     }
 36 
 37     public void close() {
 38         sta.close();
 39     }
 40 
 41     public void run() {
 42         sta.run();
 43     }
 44 
 45     public state getSta() {
 46         return sta;
 47     }
 48 
 49     public opening getOpen() {
 50         return open;
 51     }
 52 
 53     public void setOpen(opening open) {
 54         this.open = open;
 55     }
 56 
 57     public closeing getClose() {
 58         return close;
 59     }
 60 
 61     public void setClose(closeing close) {
 62         this.close = close;
 63     }
 64 
 65     publicgetRun Runing with () {
 66          return RUN;
 67      }
 68  
69      public  void setRun (Runing with RUN) {
 70          the this .run = RUN;
 71 is      }
 72  
73 is  }
 74  
75  // state 
76  abstract  class State {
 77      // for changing the state 
78      Private context CON;
 79  
80      public context getCon () {
 81          return CON;
 82      }
 83  
84      public void setCon(context con) {
 85         this.con = con;
 86     }
 87 
 88     public abstract void open();
 89 
 90     public abstract void close();
 91 
 92     public abstract void run();
 93 }
 94 
 95 //具体行为
 96 class opening extends state {
 97 
 98     @Override
 99     public void open() {
100         System.out.println ( "Error status" );
 101      }
 102  
103      @Override
 104      public  void Close () {
 105          System.out.println ( "Close" );
 106          context CON = Super .getCon ();
 107          CON. setSta (con.getClose ());
 108      }
 109  
110      @Override
 111      public  void RUN () {
 112          System.out.println ( "error status" );
 113      }
 114  }
 115  
1 16  class closeingextends state {
117 
118     @Override
119     public void open() {
120         System.out.println("开启");
121         context con = super.getCon();
122         con.setSta(con.getOpen());
123     }
124 
125     @Override
126     public void close() {
127         System.out.println("错误状态");
128     }
129 
130     @Override
131     public void run() {
132         System.out.println ( "Run" );
 133          context CON = Super .getCon ();
 134          con.setSta (con.getRun ());
 135      }
 136  }
 137  
138  class Runing with the extends State {
 139  
140      @Override
 141 is      public  void Open () {
 142          System.out.println ( "error status" );
 143      }
 144  
145      @Override
 146      public  void Close () {
 147          System.out.println ( "error status" );
148     }
149 
150     @Override
151     public void run() {
152         System.out.println("正在runing");
153     }
154 }

Guess you like

Origin www.cnblogs.com/loveer/p/11286003.html