Design Patterns -- Memento

本文详细介绍了备忘录设计模式的原理与应用。通过该模式,可在不破坏封装的前提下保存和恢复对象的状态。文章探讨了备忘录、发起者及管理者三个核心角色的功能,并提供了实现建议。

The intent of the Memento Pattern is to provide storage and restoration of an object's state.  In Design Patterns(GoF) , the intent of the Memento Pattern is stated as "Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later". A memento is a tiny repository that saves an object's state. see UML:

some description:

  • Memento  (Memento)
    • stores internal state of the Originator object.
    • protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker can only pass the memento to the other objects. And 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.

The object(memento) which is to be persisted across session(object seralization)  should implement Serializable interface and the some fields initialed from files(like images) can be decleared as transient to so that these fields needn't to be saved  when persistance.

On the other hand, memento can be persisted using String. This is suitable for the context that data should be able to be modified outside the system.

At last , the words below are referred:"

Rules of thumb

  • If you only need one Memento, combine the Originator and Caretaker into one object (Brown, 1998) .
  • If you need many Mementos, store only incremental changes (Brown, 1998) . This will help to save space.
  • Memento often used in conjunction with Command, Iterator and Singleton.
  • Implementation of the Memento design pattern varies depending on the programming language. Implement the Originator as a friend class to the Memento in C++. Implement the Memento as an inner-class of the Originator in Java (Achtziger, 1999) .

"

ref: http://pages.cpsc.ucalgary.ca/~kristen/index.shtml

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值