Behavioral mode: State mode; behavioralstate

Source: Internet
Author: User

Behavioral mode: State mode; behavioralstate
1. Intention

Allows an object to change its behavior when its internal status changes. The object seems to have modified its class.

2. Alias

Status object (Objects for States)

3. Motivation

Consider a network connection class TCPConnection. The status of a TCPConnection object is in several different States: the connection has been Established (Established), The Listener is being listened (Listenling), and the connection has been Closed (Closed ). When a TCPConnection object receives a request from another object, it responds differently according to its current state. For example, the result of an Open request depends on whether the connection is closed or established. The State mode describes how TCPConnection shows different behaviors in each State.

4. Applicability

The State mode is used in the following cases:

  • The behavior of an object depends on its state, and it must change its behavior at runtime according to its State.
  • An operation contains a large number of conditional statements with multiple branches, and these branches depend on the state of the object. This state is usually represented by one or more enumerated constants. Generally, multiple operations contain the same condition structure. In State mode, each condition branch is placed in an independent class. This allows you to take the state of an object as an object based on the object's own situation. This object can be independent of other objects.
5. Structure

The core idea is: it is easy to understand when the object's status changes and its behavior changes at the same time! Take QQ For example, there are several statuses, such as online, stealth, and busy. Each status corresponds to a different operation, and your friends can also see your status. Therefore, the status mode has two points: 1. You can change the status to obtain different behaviors. 2. Your friends can see your changes at the same time. Figure:

6. Sample Code

The State class is a State class, and the Context class can be switched. Let's look at the Code ::
Implementation Code:
Status structure:

Package com. xtfggef. dp. state;/*** core class of the State class * 2012-12-1 * @ author erqing **/public class state {private String value; public String getValue () {return value ;} public void setValue (String value) {this. value = value;} public void method1 () {System. out. println ("execute the first opt! ");} Public void method2 () {System. out. println (" execute the second opt! ");}}

Implementation:

Package com. xtfggef. dp. state;/*** status mode switch class 2012-12-1 * @ author erqing **/public class Context {private State state State; public Context (state State) {this. state = state;} public State getState () {return state;} public void setState (State state) {this. state = state;} public void method () {if (state. getValue (). equals ("state1") {state. method1 ();} else if (state. getValue (). equals ("state2") {state. method2 ();}}}
Test class:

Public class Test {

Public static void main (String [] args) {State state = new State (); Context context = new Context (state); // you can specify the first state State. setValue ("state1"); context. method (); // sets the second state. setValue ("state2"); context. method ();}

}
''
Output:
Execute the first opt!
Execute the second opt!
According to this feature, the status mode is widely used in daily development. Especially when we are website users, we sometimes want to differentiate some of their functions based on certain attributes of objects, for example, simple permission control.

7. Related Modes
  • Flyweight mode: explains when and how to share State objects.
  • The status object is generally Singleton.

Reference:

Http://openhome.cc/Gossip/DesignPattern/DecoratorPattern.htm
Http://item.jd.com/10057319.html
Http://blog.csdn.net/zhangerqing/article/details/8239539

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.