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

📄 mementostructural.cs

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

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

            Originator o = new Originator(); 
            o.State = "On"; 
            
            // Store internal state 
            Caretaker c = new Caretaker(); 
            c.Memento = o.CreateMemento(); 
            
            // Continue changing originator 
            o.State = "Off"; 
            
            // Restore saved state 
            o.SetMemento(c.Memento); 
            
            // Wait for user 
            //Console.Read();
        }
    }

    // "Originator" 
    class Originator 
    {
        private string state;
        // Property 
        public string State 
        {
            get{ return state; } 
            set 
            { 
                state = value; 
                DesignPattern.FormMain.OutputInfo("State = " + state); 
            }
        }

        public Memento CreateMemento()
        { 
            return (new Memento(state)); 
        }
        public void SetMemento(Memento memento) 
        {
            DesignPattern.FormMain.OutputInfo("Restoring state:"); 
            State = memento.State; 
        }
    } 
    
    // "Memento" 
    class Memento 
    {
        private string state; 
        
        // Constructor 
        public Memento(string state) 
        {
            this.state = state; 
        }
        
        // Property 
        public string State 
        { 
            get{ return state; } 
        }
    }
    
    // "Caretaker" 
    class Caretaker 
    {
        private Memento memento; 
        
        // Property 
        public Memento Memento 
        {
            set{ memento = value; } 
            get{ return memento; } 
        }
    }
}

⌨️ 快捷键说明

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