Design Pattern 21-behavior pattern state pattern

Source: Internet
Author: User

Definition: State Pattern, which allows an object to change its behavior when its internal State changes. The object seems to have modified its class.

Type: object behavior mode

Overview:

An object has a state, so each State corresponds to some corresponding behavior. If this object has many States, there will be a lot of behaviors. Therefore, it is very complicated to judge these statuses and complete the corresponding actions based on the statuses. If you want to add a new state, you need to modify a lot of existing code. This is also against the open and closed principle. The State mode is proposed under such circumstances.

The State mode abstracts the actions corresponding to each state into separate new objects, so that the State conversion is explicit. The state transformation no longer depends on the behaviors inside the Context. In addition, the State and behavior can greatly reduce the complexity of Context objects. In addition, if a State corresponds to multiple contexts, the State can be shared by multiple Context objects.

Status, we will immediately propose that today's status is not good, and there is no effort to do anything; or today's status is good, work is good, and eat a lot. The following uses the state of a person at different times as an example to describe the state mode.

Class diagram:

Participants:

  1. Human, that is, Context calls the specific implementation of State objects through abstract interfaces.
    1. State encapsulates interfaces related to Human behaviors.
      1. Happy, Sad, which is specific to the behavior in the corresponding state.

        Sample Code:

        Using System; using System. collections. generic; using System. text; namespace Pattern21 {// abstract State public abstract class State {public abstract void Eat (); public abstract void Walk () ;}// State public class Happy when Happy: state {public override void Eat () {human. eat (); Console. writeLine ("a lot! ");} Public override void Walk () {human. Walk (); Console. WriteLine (" dancing! ");} Public void Attach (Human _ human) {human = _ human;} private Human human;} // public class Sad: state {public override void Eat () {human. eat (); Console. writeLine ("very few! ");} Public override void Walk () {human. Walk (); Console. WriteLine (" budget-consuming! ");} Public void Attach (Human _ human) {human = _ human;} private Human human;} // a person public class Human {private State current; public void SetState (State s) {current = s;} public void Eat () {Console. write ("eat");} public void Walk () {Console. write ("START");} public void Show () {current. eat (); current. walk () ;}} class Program {static void Main (string [] args) {// defines an object with many States: Human human = new Human (); // define a Happy state: Happy hState = new Happy (); hState. attach (human); human. setState (hState); human. show (); // defines a Sad state, sad Sad = new sad (); Sad. attach (human); human. setState (sad); human. show (); // You can also add a sick state. You only need to add a new class without modifying the Human class //...... console. read ();}}}

        Applicability:

        1. The behavior of an object depends on its state, and it must change its behavior according to its state at runtime.
        2. An operation contains a large number of conditional statements with multiple branches, and these branches depend on the state of the object.

          Advantages and disadvantages:

          1. Advantage: Move the status judgment logic out of the class to add a new status by adding a new class.
          2. Disadvantages: If the status is very large, there will be a lot of status classes, increasing overhead.

            References:

            1. Design Patterns-reusable Object-Oriented Software basics
            2. Big talk Design Model


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.