Design Pattern----21.Behavioral.Memento.Pattern (Delphi Sample)

Intent

  • Without violating encapsulation, capture and externalize an object’s internal state so that the object can be returned to this state later.
  • A magic cookie that encapsulates a “check point” capability.
  • Promote undo or rollback to full object status.

Problem

Need to restore an object back to its previous state (e.g. “undo” or “rollback” operations).

Discussion

The client requests a Memento from the source object when it needs to checkpoint the source object’s state. The source object initializes the Memento with a characterization of its state. The client is the “care-taker” of the Memento, but only the source object can store and retrieve information from the Memento (the Memento is “opaque” to the client and all other objects). If the client subsequently needs to “rollback” the source object’s state, it hands the Memento back to the source object for reinstatement.

An unlimited “undo” and “redo” capability can be readily implemented with a stack of Command objects and a stack of Memento objects.

The Memento design pattern defines three distinct roles:

  1. Originator - the object that knows how to save itself.
  2. Caretaker - the object that knows why and when the Originator needs to save and restore itself.
  3. Memento - the lock box that is written and read by the Originator, and shepherded by the Caretaker.

Structure

Memento scheme

Example

The Memento captures and externalizes an object’s internal state so that the object can later be restored to that state. This pattern is common among do-it-yourself mechanics repairing drum brakes on their cars. The drums are removed from both sides, exposing both the right and left brakes. Only one side is disassembled and the other serves as a Memento of how the brake parts fit together. Only after the job has been completed on one side is the other side disassembled. When the second side is disassembled, the first side acts as the Memento.

Memento example

Check list

  1. Identify the roles of “caretaker” and “originator”.
  2. Create a Memento class and declare the originator a friend.
  3. Caretaker knows when to “check point” the originator.
  4. Originator creates a Memento and copies its state to that Memento.
  5. Caretaker holds on to (but cannot peek into) the Memento.
  6. Caretaker knows when to “roll back” the originator.
  7. Originator reinstates itself using the saved state in the Memento.

Rules of thumb

  • Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.
  • Command can use Memento to maintain the state required for an undo operation.
  • Memento is often used in conjunction with Iterator. An Iterator can use a Memento to capture the state of an iteration. The Iterator stores the Memento internally.

Memento in Delphi

A Memento is an object that stores a snapshot of the internal state of another object - the memento’s originator, according to GoF.

In the following example the originator class is a class that does some calculations named TCalculator. The recall-class must be a friend class of the originator class in order to access the private variables that characterize the current state. In delphi you can realize this if the two classes are defined in the same unit.

A concrete implementation of the Memento design pattern looks like this:

 
 
  1: 
  2: 
  3: TCalculator = class 
  4: private 
  5:   ValueA,ValueB,Interval : extended; 
  6:   Strings : TStringList; 
  7: public 
  8:   //... 
  9: end;           
 10: 
 11: TCalculatorRecall = class 
 12: private 
 13:   RefObject : TCalculator; 
 14:   ValueA,ValueB,Interval : extended; 
 15:   Strings : TStringList; 
 16: public 
 17:   constructor Create(Calculator : TCalculator); 
 18:   destructor Destroy; override; 
 19: end; 
 20: 
 21: constructor TCalculatorRecall.Create(Calculator: TCalculator); 
 22: begin 
 23:   inherited Create; 
 24:   RefObject := Calculator; 
 25:   ValueA := RefObject.ValueA; 
 26:   ValueB := RefObject.ValueB; 
 27:   Interval := RefObject.Interval; 
 28:   Strings := TStringList.Create; 
 29:   Strings.Assign(RefObject.Strings); 
 30: end; 
 31: 
 32: destructor TCalculatorRecall.Destroy; 
 33: begin 
 34:   RefObject.ValueA := ValueA; 
 35:   RefObject.ValueB := ValueB; 
 36:   RefObject.Interval := Interval; 
 37:   RefObject.Strings.Assign(Strings); 
 38:   Strings.Free; 
 39:   inherited Destroy; 
 40: end; 
The following lines demonstrate how to use this class:
 
 
  1:   // Store state of object 
  2:   CalculatorRecall := TCalculatorRecall.Create(Calculator); 
  3: 
  4:   // Change the state of object to do some calculations 
  5:   Calculator.ValueA := ... 
  6:   Calculator.DoSomething; 
  7: 
  8:   // Restore the original state 
  9:   CalculatorRecall.Destroy;

Examples from the VCL for the Memento Design Pattern are TFontRecall, TPenRecall and tBrushRecall, three new available classes in Delphi 6 that are derived from TRecall. Of course you can also define your own classes in this way. If you do this, consider two important points:

  • Derive the originator class from TPersistent
  • Implement the Assign procedure

Then our example looks like this:

 
 
  1: 
  2: 
  3: TCalculator = class(TPersistent) 
  4: private 
  5:   ValueA,ValueB,Interval : extended; 
  6:   Strings : TStringList; 
  7:   //... 
  8: public 
  9:   //... 
 10:   procedure Assign(Source: TPersistent); override; 
 11:   //... 
 12: end; 
 13: 
 14: TCalculatorRecall = class(TRecall) 
 15: public 
 16:   constructor Create(ACalculator : TCalculator); 
 17: end; 
 18: 
 19: procedure TCalculator.Assign(Source: TPersistent); 
 20: var RefObject : TCalculator; 
 21: begin 
 22:   if Source is TCalculator then 
 23:   begin 
 24:     RefObject := Source as tCalculator; 
 25:     ValueA    := RefObject.ValueA; 
 26:     ValueB    := RefObject.ValueB; 
 27:     Interval  := RefObject.Interval; 
 28:     //... 
 29:     Strings.Assign(RefObject.Strings); 
 30:     //... 
 31:   end else 
 32:     inherited Assign(Source); 
 33: end; 
 34: 
 35: constructor TCalculatorRecall.Create(ACalculator: TCalculator); 
 36: begin 
 37:   inherited Create(TCalculator.Create, ACalculator); 
 38: end;

转载于:https://www.cnblogs.com/xiuyusoft/archive/2011/07/01/2095426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值