zoukankan      html  css  js  c++  java
  • 【行为型】Memento模式

        备忘录模式顾名思义就是一种能有备忘作用的设计模式,其目的是在对象外部保存其在某一时刻的状态信息,并且在任何需要的时候,都可以通过备忘录中保存的状态数据恢复对象在当时情形下的状态。

        备忘录模式旨在对象的外部保存其状态。因此,对于备忘录对象将会有一个维护者 MementoManager 对象,用于维护对象所对应的所有备忘录对象列表,并在适当的时候需要负责清除掉这些备忘录对象。模式的类结构图参考如下:

        模式的编码结构代码参考如下:

     1 namespace memento
     2 {
     3     typedef int             TData;
     4     class Memento
     5     {
     6     public:
     7         // some code here........
     8         friend class Target;
     9     private:
    10         inline const TData  getStateData() const { return m_StateData; }
    11 
    12     private:
    13         TData   m_StateData;
    14 
    15     };//class Memento
    16 
    17     class Target
    18     {
    19     public:
    20         // some code here........
    21         Memento*            createMemento() { return new (std::nothrow) Memento(); }
    22         void                setMemento(Memento* pMemento) {
    23             if (nullptr != pMemento) {
    24                 pMemento->m_StateData = this->m_StateData;
    25             }
    26         }
    27 
    28     private:
    29         TData   m_StateData;
    30 
    31     };//class Target
    32 
    33     class MementoManager
    34     {
    35     public:
    36         // some code here........
    37 
    38         ~MementoManager() { this->clearAllMementos(); }
    39 
    40         void clearAllMementos() { /*some code here........*/ }
    41 
    42     private:
    43         typedef std::deque<Memento*>    TMementoDeq;
    44 
    45         TMementoDeq m_deqMementos;
    46 
    47     };//class MementoManager
    48 
    49 }//namespace memento
    Memento模式编码结构代码参考

        备忘录模式的一些题外话:

        面向对象设计的其中一个特性就是封装,其目的就是为了尽量将对象的状态属性有效地保护起来不被外界任意修改。因此,对于前面的Target::m_StateData数据最好也是要尽量保证该(封装)特性,并且对于其Memento对象中保存的备份状态数据最好也只能够由Target专用。但是对于不同的语言,在使用备忘录模式时,是不一定都能够保证到该特性不被破坏的。像C++这样的语言,因为有friend这种语法机制的支持,它可以在该特性上得到保证。其他的一些语言如 object pascal 这样的语言就不一定能够保证了。因此,最终使用该模式时,还需要视具体的情况而定。

        备忘录模式的应用场合:

        在许多应用场合都可以使用备忘录模式。

        比如:大到游戏数据的备份,如:像Diabol这样的经典大型单机游戏,其中有个角色存档的行为,其实就可以使用备忘录模式来设计。

        比如:项目中的用户操作习惯或操作痕迹记录,也可以使用该模式。此时备忘录模式可结合本地配置来永久存档。

        比如:备忘录模式配合Command模式以及Iterator模式来实现类似Photoshop的操作历史记录面板那样的功能。

        ........

        只要理解了模式的目的意图,相信实际项目中的许多场合都能用的上它。

  • 相关阅读:
    SpringAOP-基于@AspectJ的简单入门
    SpringAOP-切面优先级
    Commons_IO_FileUtils的使用
    java_IO_装饰器
    java_IO_3
    java_IO_2
    java_IO_1
    App Inventor
    java学习_5_24
    java学习_5_23
  • 原文地址:https://www.cnblogs.com/tongy0/p/5585346.html
Copyright © 2011-2022 走看看