Design pattern behavior state pattern

Recently, I learned the state pattern in the design pattern behavior pattern, which fully reflects the nature of "polymorphism". The first is its explanation: when the internal state of an object changes, it is allowed to change its behavior, and the object looks like it has changed its class.

Why do state patterns reflect polymorphism? It can be seen from the figure that there are multiple state behaviors corresponding to specific objects, and what determines the use of this state behavior?

It is mainly through the change of the attribute value of the specific object Context. When I perform an operation (behavior) through the change of the attribute value, I decide which of the following three state behaviors to use.

The example given to the state pattern in the book "design patterns in big talk" is very vivid. The same behavior shows different effects when the time is different. Here, the time is the attribute (state) of our specific object, which corresponds to the state change in the interpretation of the state pattern, and the latter behavior is the specific action, But when the state changed, he was also changed.

To sum up, the polymorphism of this state pattern in a specific object is the implementation of a method. The behavior displayed under different attributes (States) is different, and the applicable environment of this state pattern is a relatively certain business. If you want to add a new state to an object, Then, this specific state class can only be placed in front of the original first specific state class, just like the stack. In this case, it is extensible, but compared with the method of setting upper and lower levels externally in the responsibility chain, it is still limited. However, I believe there are other ways to support its expansibility in accordance with the opening and closing principle. If so, I hope you will give me your advice. Thank you

The specific code is based on the examples in the book

Abstract state:

public abstract class State
    {
        public abstract void WriteProgram(Work w);
    }

Specific state behavior:

    //Morning working status
    public class ForenoonState : State
    {
        public override void WriteProgram(Work w)
        {
//The judgment here is to decide which behavior to show. If it does not conform to the current state, go to the next specific state class that has been set for the same operation.
            if (w.Hour < 12)
            {
                Console.WriteLine("Current time:{0}Work in the morning and be energetic",w.Hour);
            }
            else
            {
                w.SetState(new NoonState());w.WriteProgram();
            }
        }
    }
    //Noon working status
    public class NoonState : State
    {
        public override void WriteProgram(Work w)
        {
            if (w.Hour < 13)
            {
                Console.WriteLine("Current time:{0}I'm a little hungry and want to eat; Sleepy and sleepy.", w.Hour);
            }
            else
            {
                w.SetState(new AfternoonState()); w.WriteProgram();
            }
        }
    }
    //Afternoon working status
    public class AfternoonState : State
    {
        public override void WriteProgram(Work w)
        {
            if (w.Hour < 17)
            {
                Console.WriteLine("Current time:{0}I'm in good shape this afternoon. Keep working hard", w.Hour);
            }
            else
            {
                w.SetState(new EveningState()); w.WriteProgram();
            }
        }
    }
    //Working status at night
    public class EveningState : State
    {
        public override void WriteProgram(Work w)
        {
            if (w.TaskFinished)
            {
                w.SetState(new RestState());w.WriteProgram();//Turn to off duty after completing the task
            }
            else
            {
                if (w.Hour <21)
                {
                    Console.WriteLine("Current time:{0}It's time to work overtime. It's tiring to work overtime", w.Hour);
                }
                else 
                {
                    w.SetState(new SleepingState()); w.WriteProgram();//After 21 o'clock, turn to sleep
                }
            }
        }
    }

    //Sleep state
    public class SleepingState:State
    {
        public override void WriteProgram(Work w)
        {
            Console.WriteLine("Current time:{0}I can't. I'm asleep", w.Hour);
        }
    }
    //Off duty rest status
    public class RestState : State
    {
        public override void WriteProgram(Work w)
        {
            Console.WriteLine("Current time:{0}I can't. I'm asleep", w.Hour);
        }
    }

Specific work category:

    public class Work
    {
        private State current;
        
        public Work()
        {
            current = new ForenoonState();	//Initialize to start work at 9 a.m
        }
        private double hour;
        public double Hour	//Small clock, the basis of state transition
        {
            get { return hour; }
            set { hour = value; }
        }
        private bool finish = false;
        public bool TaskFinished	//The basis for completing the task attribute and whether you can get off work
        {
            get { return finish; }
            set { finish = value; }
        }
//This method is mainly to bring in the specific state class, and then let the following methods use it
        public void SetState(State s)	//Get status
        {
            current = s;
        }
//The following method has not changed from beginning to end. The change is its internal specific display value.
        public void WriteProgram() 
        {
            current.WriteProgram(this);
        }
    }

 

 

 

Posted by waseembari1985 on Sat, 30 Oct 2021 16:54:55 -0700