分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

Participants

The classes and/or objects participating in this pattern are:

  • Memento (Memento)

    • Stores internal state of the Originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion.
    • Protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface to the Memento -- it can only pass the memento to the other objects. Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator that produces the memento would be permitted to access the memento's internal state.
  • Originator (SalesProspect) 
    • Creates a memento containing a snapshot of its current internal state.
    • Uses the memento to restore its internal state
  • Caretaker (Caretaker) 
    • Is responsible for the memento's safekeeping
    • Never operates on or examines the contents of a memento.

Sample Code in C#


This structural code demonstrates the Memento pattern which temporary saves and restores another object's internal state.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Memento Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Structural Memento Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            var o = new Originator { State = "On" };            // Store internal state            var c = new Caretaker { Memento = o.CreateMemento() };            // Continue changing originator            o.State = "Off";            // Restore saved state            o.SetMemento(c.Memento);        }        #endregion    }    /// <summary>    /// The 'Originator' class    /// </summary>    internal class Originator    {        #region Fields        /// <summary>        /// The state.        /// </summary>        private string state;        #endregion        // Property        #region Public Properties        /// <summary>        /// Gets or sets the state.        /// </summary>        public string State        {            get            {                return this.state;            }            set            {                this.state = value;                Console.WriteLine("State = " + this.state);            }        }        #endregion        // Creates memento         #region Public Methods and Operators        /// <summary>        /// The create memento.        /// </summary>        /// <returns>        /// The <see cref="Memento"/>.        /// </returns>        public Memento CreateMemento()        {            return new Memento(this.state);        }        // Restores original state        /// <summary>        /// The set memento.        /// </summary>        /// <param name="memento">        /// The memento.        /// </param>        public void SetMemento(Memento memento)        {            Console.WriteLine("Restoring state...");            this.State = memento.State;        }        #endregion    }    /// <summary>    /// The 'Memento' class    /// </summary>    internal class Memento    {        #region Fields        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Memento"/> class.        /// </summary>        /// <param name="state">        /// The state.        /// </param>        public Memento(string state)        {            this.State = state;        }        #endregion        // Gets or sets state        #region Public Properties        /// <summary>        /// Gets the state.        /// </summary>        public string State { get; private set; }        #endregion    }    /// <summary>    /// The 'Caretaker' class    /// </summary>    internal class Caretaker    {        // Gets or sets memento        #region Public Properties        /// <summary>        /// Gets or sets the memento.        /// </summary>        public Memento Memento { get; set; }        #endregion    }}// Output:/*State = OnState = OffRestoring state:State = On*/

This real-world code demonstrates the Memento pattern which temporarily saves and then restores the Sales Prospect's internal state.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Memento Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Real-World Memento Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            var s = new SalesProspect { Name = "Noel van Halen", Phone = "(412) 256-0990", Budget = 25000.0 };            // Store internal state            var m = new ProspectMemory { Memento = s.SaveMemento() };            // Continue changing originator            s.Name = "Leo Welch";            s.Phone = "(310) 209-7111";            s.Budget = 1000000.0;            // Restore saved state            s.RestoreMemento(m.Memento);        }        #endregion    }    /// <summary>    /// The 'Originator' class    /// </summary>    internal class SalesProspect    {        #region Fields        /// <summary>        /// The budget.        /// </summary>        private double budget;        /// <summary>        /// The name.        /// </summary>        private string name;        /// <summary>        /// The phone.        /// </summary>        private string phone;        #endregion        #region Public Properties        /// <summary>        /// Gets or sets the budget.        /// </summary>        public double Budget        {            get            {                return this.budget;            }            set            {                this.budget = value;                Console.WriteLine("Budget: " + this.budget);            }        }        /// <summary>        /// Gets or sets the name.        /// </summary>        public string Name        {            get            {                return this.name;            }            set            {                this.name = value;                Console.WriteLine("Name:  " + this.name);            }        }        /// <summary>        /// Gets or sets the phone.        /// </summary>        public string Phone        {            get            {                return this.phone;            }            set            {                this.phone = value;                Console.WriteLine("Phone: " + this.phone);            }        }        #endregion        // Restores memento        #region Public Methods and Operators        /// <summary>        /// The restore memento.        /// </summary>        /// <param name="memento">        /// The memento.        /// </param>        public void RestoreMemento(Memento memento)        {            Console.WriteLine("\nRestoring state --\n");            this.Name = memento.Name;            this.Phone = memento.Phone;            this.Budget = memento.Budget;        }        /// <summary>        /// The save memento.        /// </summary>        /// <returns>        /// The <see cref="Memento"/>.        /// </returns>        public Memento SaveMemento()        {            Console.WriteLine("\nSaving state --\n");            return new Memento(this.name, this.phone, this.budget);        }        #endregion    }    /// <summary>    /// The 'Memento' class    /// </summary>    internal class Memento    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Memento"/> class.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        /// <param name="phone">        /// The phone.        /// </param>        /// <param name="budget">        /// The budget.        /// </param>        public Memento(string name, string phone, double budget)        {            this.Name = name;            this.Phone = phone;            this.Budget = budget;        }        #endregion        #region Public Properties        /// <summary>        /// Gets or sets the budget.        /// </summary>        public double Budget { get; set; }        /// <summary>        /// Gets or sets the name.        /// </summary>        public string Name { get; set; }        /// <summary>        /// Gets or sets the phone.        /// </summary>        public string Phone { get; set; }        #endregion        // Gets or sets budget    }    /// <summary>    /// The 'Caretaker' class    /// </summary>    internal class ProspectMemory    {        // Property        #region Public Properties        /// <summary>        /// Gets or sets the memento.        /// </summary>        public Memento Memento { get; set; }        #endregion    }}// Output:/*Name:  Noel van HalenPhone: (412) 256-0990Budget: 25000Saving state --Name:  Leo WelchPhone: (310) 209-7111Budget: 1000000Restoring state --Name:  Noel van HalenPhone: (412) 256-0990Budget: 25000*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Memento(C#)相关推荐

  1. Design Pattern - Visitor(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  2. Design Pattern - State(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  3. Design Pattern - Observer(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  4. Design Pattern - Mediator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  5. Design Pattern - Iterator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  6. Design Pattern - Interpreter(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  7. Design Pattern - Command (C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  8. Design Pattern - Flyweight(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  9. Design Pattern - Proxy(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

最新文章

  1. 华为畅享8的悬浮窗在哪里_华为畅享8悬浮球设置 | 手游网游页游攻略大全
  2. 6.Java集成开发环境
  3. 2.1.1 物理层接口特性、数据通信模型、物理层基本概念(数据、信号、码元 、信源、信道、信宿 、速率、波特、带宽)
  4. 端云互联 3.0 击破云原生开发的痛点
  5. C++ 虚函数与纯虚函数
  6. Bigo 实时计算平台建设实践
  7. Spark 调用 hive使用动态分区插入数据
  8. OpenYurt开箱测评|一键让原生K8s集群具备边缘计算能力
  9. python程序—名片管理系统
  10. java启动线程时 extends与implements的一个差异
  11. ---单元数组-创建获取重塑单元数组----求解形如A(B)
  12. windows安装使用SQLlite并在C#调用SQLlite开发
  13. HDU 6274 Master of Sequence (暴力+下整除)
  14. WinForm界面开发教程——图文并茂的界面设计
  15. html中的css样式表达式,CSS表达式
  16. npm WARN deprecated bfj-node4@5.3.1: Switch to the `bfj` package for fixes and new features
  17. c语言第五次作业-指针-总结博客
  18. 医院招聘护士 计算机证,医院招聘护士面试自我介绍
  19. WannaCry勒索病毒分析 **下**
  20. LA4043 KM算法

热门文章

  1. JS Nice – JavaScript 代码美化和格式化工具
  2. Windows平台SSH登录Linux并使用图形化界面
  3. iOS 快速定位到系统设置界面
  4. 阿里云盾技术强在哪里?轻松防御DDoS、CC攻击
  5. RedHat已更改其开源许可规则
  6. 易宝典——玩转O365中的EXO服务 之四十 创建就地电子数据展示搜索
  7. OpenERP Web开发
  8. oracle数据源的报表sql计算慢解决
  9. sublime tex创建可服用的片段
  10. 基于catalog 创建RMAN存储脚本