C # Design Patterns --- state mode

State mode Profile

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

State model to solve the code contains a large number of conditional statements about the status of the object.
Here Insert Picture Description

advantage: Disadvantages:
1. Packaging transformation rules 1. state pattern using the system inevitably increases the number of classes and objects
2. The enumeration of possible states, before the enumeration state needs to determine the status of species 2. The structure and implementation of the state model are more complex, if used improperly will lead to confusion and code of the program structure
3. All with a state-related behavior into a class, and you can easily add new state, only need to change the state of an object can change the behavior of objects 3. Status mode support "on-off principle" is not very good for the state of the mode can be switched states, adding new classes need to modify the source code status of those responsible for state transitions, or can not switch to the new state, and modify a behavior state of the class corresponding to the class also need to modify the source code
4. Allow the state transition logic state of the object in one, rather than a block of a conditional statement huge /
The environment allows multiple objects share a state object, thereby reducing the number of objects in the system. /

C # template status Demo mode

Here Insert Picture Description

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("当前状态:{0}", State.GetType().Name ); }
        }

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

Test Results:
Here Insert Picture Description

C # achieve qq mailbox greeting calendar

Here Insert Picture Description

using System;
namespace StatePattern
{
    /// <summary>
    /// 状态模式   实现qq邮箱  时间问候语显示
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context(20);                 // 设置时间 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("早上好");
            }
            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("下午好");
            }
            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("晚上好");
            }
            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("当前状态:{0}", State.GetType().Name ); }
        }

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

Here Insert Picture Description
Generally only one method Interface command mode. While one or more of the interface state mode. Further, the class method implementation state mode, usually the return value, or change the value of the instance variable. In other words, the state and the state of the object model is generally related. The method of implementation class have different functions, a method of covering the interface. State mode and command mode the same can also be used to eliminate other conditions if ... else selection statement.

Reference material

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

Published 22 original articles · won praise 20 · views 2950

Guess you like

Origin blog.csdn.net/chasinghope/article/details/104262441