备忘录模式(Memento)

1.    定义

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

2.      UML 类图

 

3.      结构代码

// Memento pattern -- Structural example

using System;

 

namespace DoFactory.GangOfFour.Memento.Structural

{

  ///<summary>

  /// MainApp startup class for Structural

  /// Memento Design Pattern.

  ///</summary>

  classMainApp

  {

    ///<summary>

    /// Entry point into console application.

    ///</summary>

    staticvoid Main()

    {

      Originator o =new Originator();

      o.State = "On";

 

      // Store internal state

      Caretaker c =new Caretaker();

      c.Memento = o.CreateMemento();

 

      // Continue changing originator

      o.State = "Off";

 

      // Restore saved state

      o.SetMemento(c.Memento);

 

      // Wait for user

      Console.ReadKey();

    }

  }

 

  ///<summary>

  /// The 'Originator' class

  ///</summary>

  classOriginator

  {

    privatestring _state;

 

    // Property

    publicstring State

    {

      get {return _state; }

      set

      {

        _state = value;

        Console.WriteLine("State = " + _state);

      }

    }

 

    // Creates memento

    publicMemento CreateMemento()

    {

      return (newMemento(_state));

    }

 

    // Restores original state

    publicvoid SetMemento(Memento memento)

    {

      Console.WriteLine("Restoring state...");

      State = memento.State;

    }

  }

 

  ///<summary>

  /// The 'Memento' class

  ///</summary>

  classMemento

  {

    privatestring _state;

 

    // Constructor

    public Memento(string state)

    {

      this._state = state;

    }

 

    // Gets or sets state

    publicstring State

    {

      get {return _state; }

    }

  }

 

  ///<summary>

  /// The 'Caretaker' class

  ///</summary>

  classCaretaker

  {

    privateMemento _memento;

 

    // Gets or sets memento

    publicMemento Memento

    {

      set { _memento =value; }

      get {return _memento; }

    }

  }

}


Output
State = On
State = Off
Restoring state:
State = On

4.      实例代码

// Memento pattern -- Real World example

using System;

 

namespace DoFactory.GangOfFour.Memento.RealWorld

{

  ///<summary>

  /// MainApp startup class for Real-World

  /// Memento Design Pattern.

  ///</summary>

  classMainApp

  {

    ///<summary>

    /// Entry point into console application.

    ///</summary>

    staticvoid Main()

    {

      SalesProspect s =new SalesProspect();

      s.Name = "Noel van Halen";

      s.Phone = "(412) 256-0990";

      s.Budget = 25000.0;

 

      // Store internal state

      ProspectMemory m =new ProspectMemory();

      m.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);

 

      // Wait for user

      Console.ReadKey();

    }

  }

 

  ///<summary>

  /// The 'Originator' class

  ///</summary>

  classSalesProspect

  {

    privatestring _name;

    privatestring _phone;

    privatedouble _budget;

 

    // Gets or sets name

    publicstring Name

    {

      get {return _name; }

      set

      {

        _name = value;

        Console.WriteLine("Name:  " + _name);

      }

    }

 

    // Gets or sets phone

    publicstring Phone

    {

      get {return _phone; }

      set

      {

        _phone = value;

        Console.WriteLine("Phone: " + _phone);

      }

    }

 

    // Gets or sets budget

    publicdouble Budget

    {

      get {return _budget; }

      set

      {

        _budget = value;

        Console.WriteLine("Budget: " + _budget);

      }

    }

 

    // Stores memento

    publicMemento SaveMemento()

    {

      Console.WriteLine("\nSaving state --\n");

      returnnew Memento(_name, _phone, _budget);

    }

 

    // Restores memento

    publicvoid RestoreMemento(Memento memento)

    {

      Console.WriteLine("\nRestoring state --\n");

      this.Name = memento.Name;

      this.Phone = memento.Phone;

      this.Budget = memento.Budget;

    }

  }

 

  ///<summary>

  /// The 'Memento' class

  ///</summary>

  classMemento

  {

    privatestring _name;

    privatestring _phone;

    privatedouble _budget;

 

    // Constructor

    public Memento(string name,string phone, double budget)

    {

      this._name = name;

      this._phone = phone;

      this._budget = budget;

    }

 

    // Gets or sets name

    publicstring Name

    {

      get {return _name; }

      set { _name =value; }

    }

 

    // Gets or set phone

    publicstring Phone

    {

      get {return _phone; }

      set { _phone =value; }

    }

 

    // Gets or sets budget

    publicdouble Budget

    {

      get {return _budget; }

      set { _budget =value; }

    }

  }

 

  ///<summary>

  /// The 'Caretaker' class

  ///</summary>

  classProspectMemory

  {

    privateMemento _memento;

 

    // Property

    publicMemento Memento

    {

      set { _memento =value; }

      get {return _memento; }

    }

  }

}


Output
Name:   Noel van Halen
Phone:  (412) 256-0990
Budget: 25000

Saving state --

Name:   Leo Welch
Phone:  (310) 209-7111
Budget: 1000000

Restoring state --

Name:   Noel van Halen
Phone:  (412) 256-0990
Budget: 25000

该文章来自:http://www.dofactory.com/Patterns/PatternMemento.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值