C ා design mode -- state mode

Introduction to state mode

When an object's internal state changes, it is allowed to change its behavior. When the object looks like it changes its class. State pattern can be realized: the behavior of an object depends on its state (property), and its related behavior can be changed according to its state change.

The state pattern solves the problem that the code contains a large number of conditional statements related to the object state.

Advantage: Disadvantages:
1. Encapsulation of transformation rules 1. The use of state mode will inevitably increase the number of system classes and objects
2. Enumerate possible states. Before enumerating States, you need to determine the state type 2. The structure and implementation of state mode are complex. Improper use of state mode will lead to confusion of program structure and code
3. Put all the behaviors related to a state into a class, and easily add new states. Only changing the state of the object can change the behavior of the object 3. The state mode does not support the "open close principle". For the state mode that can be switched, adding a new state class requires modifying the source code responsible for the state transition, otherwise it cannot be switched to the new state, and modifying the behavior of a state class also requires modifying the source code of the corresponding class
4. Allow state transformation logic and state object to be integrated, rather than a huge conditional statement block /
5. Multiple environment objects can share a state object, thus reducing the number of objects in the system. /

C? State mode Demo template

using System;
namespace StatePattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context();
            context.State = new ConcreteState1();

            context.Request();
            context.Request();
            context.Request();
            context.Request();
            context.Request();
            context.Request();
            context.Request();
            context.Request();
            context.Request();

        }
    }

    public abstract class AbstractState
    {
        public abstract void Handler(Context context);
    }

    public class ConcreteState1 : AbstractState
    {
        public override void Handler(Context context)
        {
            context.State = new ConcreteState2();
        }
    }

    public class ConcreteState2 : AbstractState
    {
        public override void Handler(Context context)
        {
            context.State = new ConcreteState3();
        }
    }

    public class ConcreteState3 : AbstractState
    {
        public override void Handler(Context context)
        {
            context.State = new ConcreteState1();
        }
    }

    public class Context
    {
        private AbstractState state;
        public AbstractState State
        {
            get { return state; }
            set { state = value; Console.WriteLine("Current status:{0}", State.GetType().Name ); }
        }

        public void Request()
        {
            this.State.Handler(this);
        }
    }    
}

Test results:

C ා implementation of qq email calendar greetings

using System;
namespace StatePattern
{
    /// <summary>
    ///State mode to display time greetings of qq mailbox
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context(20);                 // Setting time Hour=20;
            context.State = new MorningState();
              
            context.Request();
            Console.WriteLine("----------------------");

            context.Hour = 15;
            context.Request();
            Console.WriteLine("----------------------");

            context.Hour = 10;
            context.Request();
            Console.WriteLine("----------------------");

        }
    }

    public abstract class AbstractState
    {
        public abstract void Handler(Context context);
    }

    public class MorningState : AbstractState
    {
        public override void Handler(Context context)
        {
            if( context.Hour%24 > 9 && context.Hour%24 <= 12)
            {
                Console.WriteLine("Good morning");
            }
            else
            {
                context.State = new AfternoonState();
                context.Request();
            }           
        }
    }

    public class AfternoonState : AbstractState
    {
        public override void Handler(Context context)
        {
            if (context.Hour % 24 > 12 && context.Hour % 24 <= 18)
            {
                Console.WriteLine("Good afternoon");
            }
            else
            {
                context.State = new EveningState();
                context.Request();
            }
        }
    }

    public class EveningState : AbstractState
    {
        public override void Handler(Context context)
        {
            if (context.Hour % 24 > 18 && context.Hour % 24 < 24)
            {
                Console.WriteLine("Good evening");
            }
            else
            {
                context.State = new MorningState();
                context.Request();
            }
        }
    }

    public class Context
    {
        private int hour;
        public int Hour
        {
            get; set;
        }

        public Context(int nowHour)
        {
            this.Hour = nowHour;
        }

        
        private AbstractState state;
        public AbstractState State
        {
            get { return state; }
            set { state = value; Console.WriteLine("Current status:{0}", State.GetType().Name ); }
        }

        public void Request()
        {
            this.State.Handler(this);
        }
    }    
}


Usually there is only one method in the interface of command mode. There are one or more methods in the state mode interface. In addition, the method of the implementation class of the state mode generally returns a value, or changes the value of the instance variable. In other words, the state pattern is generally related to the state of the object. The methods that implement the class have different functions, covering the methods in the interface. Like command mode, state mode can also be used to eliminate if else and other conditional selection statements.

Reference material

https://m.runoob.com/design-pattern/state-pattern.html
https://www.bilibili.com/video/av78515440?p=16

22 original articles published, 20 praised, 2918 visited
Private letter follow

Posted on Tue, 11 Feb 2020 05:06:53 -0500 by devsanctum