Traditional design pattern (10) State mode

Source: Internet
Author: User

Talking about state mode what we're talking about here is a common technique: how to model the state within an object----to hold the state value by creating an instance variable and writing the conditional code within the method to handle the different states. We want to put all the behaviors of a state in a class. So we localize the behavior and make things easier to change and understand.

The bug still takes the order system.

<summary>///State Interface///</summary> public interface Simplestate {//Check cart
        void Checkcart ();
        Check account void Checkacount ();
    Submit order void Ordersumbit ();
        public class Order {//initial state simplestate initsate;
        Pending review status Simplestate Pendingsate;
    
        Post-submission status Simplestate submittedsate;
            Public order () {initsate = new initstate (this);
            Pendingsate = new Pendingsate (this);
        Submittedsate = new Submittedsate (this);
    
        Private simplestate State;
        public void SetState (Simplestate state) {this.state = state;
        Public Simplestate Getinitstate () {return initsate;
        Public Simplestate Getpendingsate () {return pendingsate;
  Public Simplestate getsubmittedsate () {          return submittedsate;
        } public class Initstate:simplestate {order;
        Public Initstate (order) {this.order = order;
            public void Checkcart () {Console.WriteLine ("Order into initial state");
        Order.setstate (Order.getinitstate ());
            public void Checkacount () {Console.WriteLine ("Order enters pending approval status");
        Order.setstate (Order.getpendingsate ());           
        public void Ordersumbit () {Console.WriteLine ("This state cannot enter this step");
        } public class Pendingsate:simplestate {order;
        Public Pendingsate (order) {this.order = order;
        public void Checkcart () {Console.WriteLine ("This state cannot enter this step");
     public void Checkacount () {Console.WriteLine ("Order enters pending approval status");       Order.setstate (Order.getpendingsate ());
            public void Ordersumbit () {Console.WriteLine ("Order into submit status");
        Order.setstate (Order.getsubmittedsate ());
        } public class Submittedsate:simplestate {order;
        Public Submittedsate (order) {this.order = order;
        public void Checkcart () {Console.WriteLine ("This state cannot enter this step");       
        public void Checkacount () {Console.WriteLine ("This state cannot enter this step");
            public void Ordersumbit () {Console.WriteLine ("Order into submit status");
        Order.setstate (Order.getsubmittedsate ()); }
    }

Summary: The state pattern allows an object to change its behavior as the internal state changes, and the object appears to modify its class. Because this pattern encapsulates the state as a separate class and delegates the action to the object representing the current state, we know that the behavior changes as the internal state changes. From a customer's point of view, if the object you are using can completely change its behavior, you will feel that the object is actually instantiated from another class. In fact, however, we are using a combination to create the illusion of a class change by simply referencing different state objects.

This article address: http://www.bianceng.cn/Programming/project/201602/49598.htm

Author:cnblogs bugs that stay up late

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.