1. 程式人生 > >狀態模式(State)

狀態模式(State)

request world 修改 分享圖片 core killed trac concrete pro

模式定義

狀態模式(State Pattern) :允許一個對象在其內部狀態改變時改變它的行為,對象看起來似乎修改了它的類。

UML類圖

技術分享圖片

  • 環境類(Context): 定義一個接口,用以封裝環境對象的一個特定的狀態所對應的行為。
  • 抽象狀態類(State): 每一個具體狀態類都實現了環境的一個狀態所對應的行為。
  • 具體狀態類(ConcreteState):定義客戶端所感興趣的接口,並且保留一個具體狀態類的實例。這個具體狀態類的實例給出此環境對象的現有狀態。

    代碼結構

    public class StateApp
    {
        public void Run()
        {
            Context c = new Context(new ConcreteStateA());
    
            c.Request();
            c.Request();
            c.Request();
    
            Console.ReadKey();
        }
    }
    
    abstract class State
    {
        public abstract void Handle(Context context);
    }
    
    class ConcreteStateA : State
    {
        public override void Handle(Context context)
        {
            context.State = new ConcreteStateB();
        }
    }
    
    class ConcreteStateB : State
    {
        public override void Handle(Context context)
        {
            context.State = new ConcreteStateA();
        }
    }
    
    class Context
    {
        public State State { get; set; }
    
        public Context(State state)
        {
            this.State = state;
        }
        public void Request()
        {
            this.State.Handle(this);
        }
    }

    情景案例

    那前幾天火的微信小遊戲“頭腦王者”距離吧!

    public class StateRealWorldApp
    {
        public void Run()
        {
            Player player = new Player("小王");
            player.Display();
    
            for (int i = 0; i < 5; i++)
            {
                player.Win();
            }
            player.Display();
    
            Console.ReadKey();
        }
    }
    
    abstract class Grade
    {
        protected int Score = 0;
        public string Name = string.Empty;
        public Player Player { get; set; }
        public abstract void Win();
        public abstract void Lose();
    }
    
    class New : Grade
    {
        public New(Player player)
        {
            this.Player = player;
            this.Name = "入手新門";
        }
    
        public override void Win()
        {
            this.Score = this.Score + 1;
            if(this.Score == 5)
            {
                this.Player.State = new Skilled(this);
                this.Score = 0;
            }
        }
    
        public override void Lose()
        {
            if(this.Score != 0)
            {
                this.Score = this.Score - 1;
            }
        }
    }
    
    class Skilled : Grade
    {
        public Skilled(Grade state) 
        {
            this.Player = state.Player;
            this.Name = "起步熟手";
        }
        public override void Win()
        {
            this.Score = this.Score + 1;
            if (this.Score == 7)
            {
                this.Player.State = new BlackIron(this);
                this.Score = 0;
            }
        }
    
        public override void Lose()
        {
            if (this.Score != 0)
            {
                this.Score = this.Score - 1;
            }
        }
    }
    
    class BlackIron : Grade
    {
        public BlackIron(Grade state)
        {
            this.Player = state.Player;
            this.Name = "堅韌黑鐵";
        }
        public override void Win()
        {
            this.Score = this.Score + 1;
            if (this.Score == 7)
            {
                this.Score = 0;
            }
        }
    
        public override void Lose()
        {
            if (this.Score != 0)
            {
                this.Score = this.Score - 1;
            }
        }
    }
    
    class Player
    {
        public Grade State { get; set; }
    
        private string Name;
    
        public Player(string name)
        {
            this.State = new New(this);
            this.Name = name;
        }
    
        public void Win()
        {
            this.State.Win();
        }
    
        public void Lose()
        {
            this.State.Lose();
        }
    
        public void Display()
        {
            Console.WriteLine("用戶:{0},等級為:{1}",this.Name,this.State.Name);
        }
    }

狀態模式(State)