`

MementoPattern

 
阅读更多

意图
在不破坏封装性的前提下,捕获一个对象内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态.

 

适用性
必须保存一个对象在某一个时刻的(部分)状态,这样以后需要时它才能恢复到先前的状态.

 

构成:
1.备忘录(Memento)角色:保持原发器(Originator)的内部状态,根据原发器来决定保存哪些内部的状态.(备忘录角色就好比是一个日记本把当前发生的一些事情记录下来,当过段时间回过头来再看日记本的时候就知道在那个时刻发生了什么事情)

备忘录可以有效地利用两个接口.看管者只能调用狭窄(功能有限)的接口---它只能传递备忘录给其他对象.而原发器可以调用一个宽阔(功能强大)的接口,通过这个接口可以访问所有需要的数据,使原发器可以返回先前的状态.

 

2.原发器(Originator)角色:创建一个备忘录,记录它当前内部状态.可以利用一个备忘录来回复它的内部状态.

 

3.看管者(Caretaker)角色:只负责看管备忘录.不可以对备忘录的内容操作或检查.

 

ClassDiagram:

 

SequenceDiagram:

 

 //公司销售记录示例
    class Client
    {
        static void Main(string[] args)
        {
            Originator ori = new Originator("张三", "123456", 1000);

            ori.Show();

            Caretaker caretaker = new Caretaker();

            //保存备忘录
            caretaker.Memento = ori.SaveMemento();

            ori = new Originator("李四", "654321", 2000);

            ori.Show();

            //恢复备忘录
            ori.RestoreMemento(caretaker.Memento);

            ori.Show();

            Console.ReadKey();
        }
    }

    /// <summary>
    /// 备忘录角色
    /// </summary>
    class Memento
    {
        private string name;
        private string phone;
        private double budget;

        public Memento(string name, string phone, double budget)
        {
            this.name = name;
            this.phone = phone;
            this.budget = budget;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        public double Budget
        {
            get { return budget; }
            set { budget = value; }
        }
    }

    /// <summary>
    /// 原发器角色
    /// </summary>
    class Originator
    {
        private string name;
        private string phone;
        private double budget;

        public Originator(string name, string phone, double budget)
        {
            this.name = name;
            this.phone = phone;
            this.budget = budget;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        public double Budget
        {
            get { return budget; }
            set { budget = value; }
        }

        //保存备忘录
        public Memento SaveMemento()
        {
            return new Memento(name, phone, budget);
        }
        //恢复备忘录
        public void RestoreMemento(Memento memento)
        {
            name = memento.Name;
            phone = memento.Phone;
            budget = memento.Budget;
        }

        public void Show()
        {
            Console.WriteLine("销售期望");
            Console.WriteLine("姓名: {0}", name);
            Console.WriteLine("电话: {0}", phone);
            Console.WriteLine("预算: {0}", budget);
        }
    }

    /// <summary>
    /// 看管者
    /// </summary>
    class Caretaker
    {
        private Memento memento;

        internal Memento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
    }

 

 

  • 大小: 43.7 KB
  • 大小: 85 KB
分享到:
评论

相关推荐

    设计模式之备忘录模式(Memento Pattern)

    在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到保存的状态。

    C#备忘录模式(Memento Pattern)实例教程

    本文以一个简单实例讲述了C#备忘录模式(Memento Pattern)的实现方法。分享给大家供大家参考。具体实现方法如下: 简单来说,备忘录模式就是支持回退操作。假设让一个Notepad支持回退操作,如何实现呢? 首先需要一个...

    深入浅出设计模式——备忘录模式(MementoPattern)

    为了使软件的使用更加人性化,对于误操作,我们需要提供一种类似“后悔药”的机制,让软件系统可以回到误操作前的状态,因此需要保存用户每一次操作时系统的状态,一旦出现误操作,可以把存储的历史状态取出即可回到...

    C#版 24种设计模式

    备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...

    C#设计模式_设计模式_C#_

    创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3.... 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)

    23种设计模式 (创建型,结构型,行为型)

    创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) ... 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)

    设计模式代码——c#

    C#设计模式(23种设计模式) 1. 单件模式(Singleton Pattern) ...20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)

    C#设计模式(23种设计模式)

    创建型: 1. 单件模式(Singleton Pattern) ... 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern) @Author kwming

    用Java实现23种设计模式

    备忘录模式(Memento Pattern) 观察者模式(Observer Pattern) 状态模式(State Pattern) 空对象模式(Null Object Pattern) 策略模式(Strategy Pattern) 模板模式(Template Pattern) 访问者模式...

    设计模式PPT

     备忘录模式(Memento Pattern)  观察者模式(Observer Pattern)  状态模式(State Pattern)  策略模式(Strategy Pattern)  模板方法模式(Template Method Pattern)  访问者模式(Visitor ...

    32种设计模式

    C# 32种设计模式: 创建型: 1. 单件模式(Singleton ... 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)

    33种JAVA设计模式DEMO

    备忘录模式(Memento Pattern) 观察者模式(Observer Pattern) 状态模式(State Pattern) 空对象模式(Null Object Pattern) 策略模式(Strategy Pattern) 模板模式(Template Pattern) 访问者模式(Visitor ...

    C#23种设计模式

    │ └─MementoPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─19.MediatorPattern │ ├─html │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ...

    常用设计模式的IOS实现源码打包下载

    AbstractFactoryPattern AdapterPattern IBridgePattern ...MementoPattern ObserverPattern PrototypePattern ProxyPattern SingletonPattern StatePattern StrategyPattern TemplatePattern VisitorPattern

    design-pattern-java.pdf

    协调多个对象之间的交互——中介者模式(三) 协调多个对象之间的交互——中介者模式(四) 协调多个对象之间的交互——中介者模式(五) 备忘录模式-Memento Pattern 撤销功能的实现——备忘录模式(一) 撤销功能...

    Apress.Pro.Design.Patterns.in.Swift

    The Swift programming... The Memento Pattern Chapter 24. The Strategy Pattern Chapter 25. The Visitor Pattern Chapter 26. The Template Method Pattern Part 5 - The MVC Pattern Chapter 27. The MVC Pattern

    Java设计模式学习教程与案例源码.zip

    5. [备忘录模式](worthed/OriginBlog/blob/master/articles/MementoPattern.md) 6. [迭代器模式](worthed/OriginBlog/blob/master/articles/IteratorPattern.md) 7. [命令模式](worthed/OriginBlog/blob/master/...

    Java.Design.Patterns.1537192353

    Java design patterns with the Simplest real world examples which are easy to understand & remember as well. Table of Contents PREFACE ABOUT DESIGN PATTERNS SINGLETON PATTERN ...MEMENTO PATTERN

Global site tag (gtag.js) - Google Analytics