分享一下我老师大神的人工智能教程!零基础,通俗易懂!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*/