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

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

【行为型】Memento模式

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

 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模式编码结构代码参考

相关文章:

  • 2021-09-07
  • 2021-12-09
  • 2022-01-15
  • 2021-09-21
  • 2022-03-05
  • 2022-12-23
  • 2021-12-20
  • 2021-09-23
猜你喜欢
  • 2022-01-03
  • 2021-05-14
  • 2021-04-29
  • 2021-11-08
  • 2022-02-22
  • 2022-02-08
  • 2022-12-23
相关资源
相似解决方案