⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statestructural.cs

📁 使用C#程序将23个常用设计模式进行列表显示
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Text;

namespace DesignPattern.StateStructural
{
    class StateStructural : AbstractPattern
    {
        public static void Run(TextBox tbInfo)
        {
            s_tbInfo = tbInfo;
            s_tbInfo.Text = "";

            // Setup context in a state 
            Context c = new Context(new ConcreteStateA()); 
            
            // Issue requests, which toggles state 
            c.Request(); 
            c.Request(); 
            c.Request(); 
            c.Request(); 
            
            // Wait for user 
            //Console.Read();
        }
    }
    
    // "State" 
    abstract class State 
    {
        public abstract void Handle(Context context); 
    }
    
    // "ConcreteStateA" 
    class ConcreteStateA : State 
    {
        public override void Handle(Context context) 
        {
            context.State = new ConcreteStateB(); 
        }
    }
    
    // "ConcreteStateB" 
    class ConcreteStateB : State 
    {
        public override void Handle(Context context) 
        {
            context.State = new ConcreteStateA(); 
        }
    } 
    
    // "Context" 
    class Context 
    {
        private State state; 
        
        // Constructor 
        public Context(State state) 
        {
            this.State = state; 
        }
        
        // Property 
        public State State 
        { 
            get { return state; } 
            set 
            {
                state = value; 
                DesignPattern.FormMain.OutputInfo("State: " + state.GetType().Name);
            }
        }
        
        public void Request() 
        {
            state.Handle(this); 
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -