zoukankan      html  css  js  c++  java
  • C# 状态模式(State)

    理解:当对象有多个状态时,把每个状态的行为写到各自的状态类里面去,当切换状态时,调用的行为也就自动切换了。 和策略模式很像,不过策略是针对外部的,而状态是针对内部的

    代码:

     using System.Windows.Forms; 
    namespace DesignMode.State
    {

        //对象类
        public class Computer
        {
            //系统要求
            private string _request;
            public string Request
            {
                get { return _request; }
                set { _request = value; }
            }


            private OperatingSystem _os;
            public OperatingSystem OS
            {
                get { return _os; }
                set { _os = value; }
            }

            public Computer(OperatingSystem os)
            {
                this._os = os;
            }

            public void Run()
            {
                //先改状态,再执行状态对应的行为
                _os.ChangeOS(this);
                _os.Run();
            }
        }

        //抽象的对象状态类
        public abstract class OperatingSystem
        {
            public abstract void ChangeOS(Computer computer);

            public abstract void Run();
        }

        public class Vista : OperatingSystem
        {
            //子状态根据对象的系统要求属性来更改对应的状态
            public override void ChangeOS(Computer computer)
            {
                if (computer.Request.Equals("W"))
                {
                    computer.OS = new Windows();
                }
                else if (computer.Request.Equals("L"))
                {
                    computer.OS = new Linux();
                }
            }

            public override void Run()
            {
                MessageBox.Show("运行Vista系统");
            }
        }

        public class Windows : OperatingSystem
        {
            public override void ChangeOS(Computer computer)
            {
                if (computer.Request.Equals("V"))
                {
                    computer.OS = new Vista();
                }
                else if (computer.Request.Equals("L"))
                {
                    computer.OS = new Linux();
                }
            }

            public override void Run()
            {
                MessageBox.Show("运行Windows系统");
            }
        }

        public class Linux : OperatingSystem
        {
            public override void ChangeOS(Computer computer)
            {
                if (computer.Request.Equals("W"))
                {
                    computer.OS = new Windows();
                }
                else if (computer.Request.Equals("V"))
                {
                    computer.OS = new Vista();
                }
            }

            public override void Run()
            {
                MessageBox.Show("运行Linux系统");
            }
        }
    }

    客户端代码:

          

       private void btn_State_Click(object sender, EventArgs e)  

           {
                DesignMode.State.OperatingSystem os = new Windows();
                Computer computer = new Computer(os);
                computer.Request = "W";
                computer.Run();
                computer.Request = "L";
                computer.Run();
                computer.Request = "V";
                computer.Run();
            }

  • 相关阅读:
    Visifire正式版(v1.1)发布
    [转]PSP机能强大!已能模拟运行WINDOWS系统?
    在Silverlight+WCF中应用以角色为基础的安全模式(一)基础篇之角色为基础的安全模式简介 Virus
    C#的加密解密算法,包括Silverlight的MD5算法 Virus
    MMORPG programming in Silverlight Tutorial (10)Implement the sprite’s 2D animation (Part IV)
    Game Script: Rescue Bill Gates
    MMORPG programming in Silverlight Tutorial (9)KeyFrame Animation
    MMORPG programming in Silverlight Tutorial (5)Implement the sprite’s 2D animation (Part II)
    MMORPG programming in Silverlight Tutorial (7)Perfect animation
    MMORPG programming in Silverlight Tutorial (3)Animate the object (Part III)
  • 原文地址:https://www.cnblogs.com/kavilee/p/2380479.html
Copyright © 2011-2022 走看看