备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在改对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

设计模式:备忘录模式

namespace Memento
{
    public class Originator
    {
        private string state;
        public string State
        {
            get { return state; }
            set { state = value; }
        }
        public Memento CreateMemento()
        {
            return new Memento(state);
        }
        public void SetMemento(Memento memento)
        {
            state = memento.State;
        }
        public void Show()
        {
            Console.WriteLine("State=" + state);
        }
    }
    public class Memento
    {
        private string state;
        public Memento(string state)
        {
            this.state = state;
        }
        public string State
        {
            get { return state; }
        }
    }
    public class Caretaker
    {
        private Memento memento;
        public Memento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
    }
}
View Code

相关文章:

  • 2021-09-19
猜你喜欢
  • 2018-10-08
  • 2021-12-19
  • 2021-09-02
  • 2021-11-27
  • 2021-07-01
相关资源
相似解决方案