Design Model State

  State definition: different state, different acts, or that every state has a corresponding behaviour. 

  When to use? 
  State model in the use of more practical for the "state of the switch." Because we often use If elseif else switch to the state, if the state against this judgement switch recurring, we will have to think whether the State can adopt the model. 

  Not only under the state but also in accordance with attributes. If an object's properties different from the conduct of the addressee is not the same this point in the database system of a relatively high frequency, we often in a data table in the rear, coupled with the property attribute meaning field for the identification records of the special nature of some of the records of such properties change (switching) and may occur at any time, it is possible to use State. 

  Whether or not to use? 
  In actual use, such as switching the status of switching are many, but sometimes not so obvious, depending on your experience and depth of understanding of the system. 

  Here, we must set out a "switch switching state" and "general state judge," there are some differences, "the general state of judgement" if there is .. elseif structure, such as: 

  If (which == 1) state = "hello"; 
  Else if (which == 2) state = "hi"; 
  Else if (which == 3) state = "bye"; 

  This is a "general state of judgement" state value is based on the different variables to determine which, and which did not state relations. Changed if: 

  If (state.euqals ( "bye")) state = "hello"; 
  Else if (state.euqals ( "hello")) state = "hi"; 
  Else if (state.euqals ( "hi")) state = "bye"; 

  This is the "switch switching state" is the state of the state from "hello" switch to "hi" and then switched to "" bye "in the switch to" hello ", as if a rotary switches, which can change state State use of the model. 

  If the above is a simple "hello "–>" hi "–>" bye "–>" hello" in one direction switch, it may not necessarily need to use the State model, because many State will be established model of type, complex , but if another act occurred: the above switch direction in turn switches, or any need to switch, we need the State. 

  NEXT cases: 

  (Public class Context 

  Private Color state = null; 

  Public void push () ( 

  / / If the current state of the red switch to blue 
  If (state == Color.red) = Color.blue state; 

  / / If the current state of the switch to blue green 
  Else if (state == Color.blue) = Color.green state; 

  / / If the current state of the black switch to red 
  Else if (state == Color.black) = Color.red state; 

  / / If the current state of the green switch to black 
  Else if (state == Color.green) = Color.black state; 

  Sample sample = new Sample (state); 
  Sample.operate (); 
  ) 

  Public void pull () ( 

  / / State and push the opposite switch 

  If (state == Color.green) = Color.blue state; 
  Else if (state == Color.black) = Color.green state; 
  Else if (state == Color.blue) = Color.red state; 
  Else if (state == Color.red) = Color.black state; 

  Sample2 sample2 = new Sample2 (state); 
  Sample2.operate (); 
  ) 

  ) 

  In the example, we have two push push and pull movement Rafah, the two switch, the Context change color, thus, we need to optimize the use of its State model. 

  Also note: But on the previous cases, state changes, but simply the color assignment, the specific actions is very simple, enormous State for the specific behavior, so, in this case, the actual use did not have to use State mode, it would increase the number of sub-categories, the simple variable complex. 

  For example: bank accounts, often in the Open and Close state of the state conversion. 

  For example: the classic TcpConnection, Tcp create a state of the closure of three interception, and repeatedly change its creation interception closed the specific behavior is not a simple 12 will be completed for the use of State 

  For example: POP mail account, there will be four state start HaveUsername Authorized quit, each state corresponding acts should be relatively large. Suitable for the use of State 

  For example: the selection of different tools in the toolbox, and can be regarded as the switch between different tools for the use of State. Graphics such as specific procedures, users can choose a different mapping tool box linear curve, this state switch can be used State. 

  State how to use the two types of entities involved in need: 

  1.state manager for state management is the switch, as in the above examples of actual Context is a state manager in the state manager in the state of the switching action. 
  2. Abstract class or interface of the parent, the state is to carry this different kind of father of different categories. 

  Context to the above example we need to amend it, and the establishment of two types of entities. 

  Step 1: First establishment of a father categories: 

  (Public abstract class State 

  Public abstract void handlepush (Context c); 
  Public abstract void handlepull (Context c); 
  Public abstract void getcolor (); 

  ) 

  Father of the method corresponds to the state manager of switching behavior in the state manager in the Context of this case is, there are two switch push and pull push pull. Well, the father of the state to deal with the two specific actions: handlepush () handlepull (); also need access to a push or pull the outcome of the method getcolor () 

  Below is the realization of specific sub-categories: 

  Public class BlueState extends State ( 

  Public void handlepush (Context c) ( 
  / / According to push approach "if the state is switching to blue green"; 
  C.setState (new GreenState ()); 

  ) 
  Public void handlepull (Context c) ( 

  / / According to pull method "If the state is the blue switch to red"; 
  C.setState (new RedState ()); 

  ) 

  Public abstract void getcolor () (return (Color.blue)) 

  ) 

  The same category of other states to achieve the same as blue. 

  Step 2: State manager should be to rewrite this is the case of Context: 

  (Public class Context 

  Private Sate state = null; / / we will be the original Color state into a new State state; 

  / / SetState state is used to change the status of the use of state setState achieve switching pulic void setState (State state) ( 

  This.state = state; 

  ) 

  Public void push () ( 

  / / State of the details of the switch, in this case it was the color changes have been encapsulated in the sub-category of handlepush achieve here without concern state.handlepush (this); 

  / / Sample to be used as a switch in the state results, the use of getColor () 
  Sample sample = new Sample (state.getColor ()); 
  Sample.operate (); 

  ) 

  Public void pull () ( 

  State.handlepull (this); 

  Sample2 sample2 = new Sample2 (state.getColor ()); 
  Sample2.operate (); 

  ) 

  ) 

  So far, it has achieved the State refactorying process. 

  These are just a very simple example, in practical application, or handelpull handlepush processing is complex. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply