Liar design mode-state mode

Source: Internet
Author: User

In state mode, the behavior of a class is based on its state change. This type of design pattern belongs to the behavioral pattern. We create objects that represent various States and a context object that behaves as the state object changes.

In the big talk design pattern, the geoscience teacher gives the definition of a state pattern: When an object's internal state changes to allow its behavior to change, the object looks like it has changed its class .

State mode mainly solves the situation when the conditional expression that controls an object state transition is too complex. It is possible to simplify the complex judgment logic by transferring the judgment logic of State to a series of classes representing different states. Of course, it is not necessary to use the "state mode" if the state is simple to judge.

The status mode structure diagram is as follows:

Structure Diagram Analysis:

1, state, abstract status class, defines an interface to encapsulate the behavior associated with a particular state of the context;

2, Concretestate class, the specific State class, each sub-class to achieve a context with a state-related behavior;

3, the context class, maintains an instance of the Concretestate subclass, this instance defines the current state;

Here is an example of an athlete starting and ending a sport, and a concrete look at the use of state patterns , such as the following:

package  com.exercise.state; /** * Abstract State interface * @author  LMB * */ public  interface  state  { public  void  doaction  (context context);}  
package  com.exercise.state; /** * Status Object startstate * @author  LMB * */ public  class  startstate  implements  State  {  @Override  public  void  doaction  (Context context) {System.out.println ( "Player is in start state." ); Context.setstate (this ); } public  String tostring  () {return  " start State "; }}
package  com.exercise.state; /** * Status Object stopstate * @author  LMB * */ public  class  stopstate  implements  State  {   @Override  public  void  doaction  (Context context) {System.out.println ( "Player is in Stop state." ); Context.setstate (this ); } public  String tostring  () {return  " stop State "; }}
package  com.exercise.state; /** * behavior changes with the state object changes the context class * @author  LMB * */  public  class     context  { public  State State; public  context     () {state = null ; } public  State getstate  () {return  state; } public  void      SetState  (state State) {this . state = State; }}
package com.exercise.state;publicclass TestState {    publicstaticvoidmain(String[] args) {        new Context();        new StartState();        startState.doAction(context);//将状态设定为开始状态        System.out.println(context.getState().toString());        new StopState();        stopState.doAction(context);//将状态设定为结束状态        System.out.println(context.getState().toString());    }}

Operation Result:

isinisinstop state.stop state

Benefits of State mode:

Localize the behavior associated with a particular state and separate the behavior of the different states. In a nutshell, the behavior of a particular state is put into an object, and since all state-related code exists in one of the states implementation classes, it is easy to add new states and transformations by defining new subclasses. The purpose of this is to eliminate large branching statements, and large branch judgments make it difficult to modify and extend them. The state mode reduces the dependency between the state sub-classes by putting the various states transfer logic together, so it is easy to maintain and extend.

Usage scenarios:

A state pattern can be considered when the behavior of an object depends on its state, and it must change its behavior according to state at run time.

Disadvantages:

1, the use of State mode will inevitably increase the number of system classes and objects.

2, the state mode structure and implementation are more complex, if improper use will lead to program structure and code confusion.

3, the state mode for the "open and close principle" support is not very good, for the state mode can be toggled state, add new state class needs to modify those responsible for state transformation of the source code, or can not switch to the new state, and modify the behavior of a State class also need to modify the corresponding class source code.

caveats : Use state mode when the behavior is constrained by state, and no more than 5 states.

Liar design mode-state mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.