Design Pattern - Memento(C#)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!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
这里写图片描述
weixin295微信小程序选课系统+ssm后端毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值