目录

《设计模式的艺术》笔记 - 备忘录模式

介绍

备忘录模式在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。它是一种对象行为模式,别名为Token。

实现

myclass.h

cpp 复制代码
//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>
#include <unordered_map>
#include <atomic>
#include <vector>
#include <memory>

class Originator;
class Memento { // 备忘录类
    friend Originator;  // 备忘录只能由Originator创建,确保安全
private:
    Memento();
    Memento(Originator *o);
    void setState(const std::string &state);
    std::string &getState();
private:
    std::string m_state;
};

class Originator {  // 原发器类,需要保存状态
public:
    Originator();

    std::shared_ptr<Memento> createMemento();

    void restoreMemento(const std::shared_ptr<Memento> &m);

    void setState(const std::string &state);

    std::string &getState();

private:
    std::string m_state;
};

class Caretaker {   // 负责人类,管理备忘录
public:
    std::shared_ptr<Memento> &getMemento();
    void setMemento(std::shared_ptr<Memento> m);

private:
    std::shared_ptr<Memento> m_memento;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

cpp 复制代码
//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"
#include <thread>
#include <unistd.h>
#include <sstream>

Memento::Memento(Originator *o) {
    m_state = o->getState();
}

void Memento::setState(const std::string &state) {
    m_state = state;
}

std::string& Memento::getState() {
    return m_state;
}

Originator::Originator() {

}

std::shared_ptr<Memento> Originator::createMemento() {
    std::shared_ptr<Memento> m(new Memento(this));
    return m;
}

void Originator::restoreMemento(const std::shared_ptr<Memento> &m) {
    setState(m->getState());
}

void Originator::setState(const std::string &state) {
    m_state = state;
}

std::string& Originator::getState() {
    return m_state;
}

void Caretaker::setMemento(std::shared_ptr<Memento> m) {
    m_memento = m;
}

std::shared_ptr<Memento>& Caretaker::getMemento() {
    return m_memento;
}

main.cpp

cpp 复制代码
#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Originator *originator = new Originator();
    originator->setState("初始状态");
    auto m0 = originator->createMemento();
    originator->setState("第一次状态");
    auto m1 = originator->createMemento();
    originator->setState("第二次状态");
    auto m2 = originator->createMemento();
    Caretaker *caretaker = new Caretaker();

    std::cout << "恢复状态前:" << std::endl;
    std::cout << "\t" << originator->getState() << std::endl;

    std::cout << "第一次恢复:" << std::endl;
    caretaker->setMemento(m2);
    originator->restoreMemento(caretaker->getMemento());
    std::cout << "\t" << originator->getState() << std::endl;

    std::cout << "第二次恢复:" << std::endl;
    caretaker->setMemento(m1);
    originator->restoreMemento(caretaker->getMemento());
    std::cout << "\t" << originator->getState() << std::endl;

    std::cout << "第三次恢复:" << std::endl;
    caretaker->setMemento(m0);
    originator->restoreMemento(caretaker->getMemento());
    std::cout << "\t" << originator->getState() << std::endl;

    delete originator;
    delete caretaker;

    return 0;
}

总结

优点

  1. 它提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤。当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原。

  2. 备忘录实现了对信息的封装。一个备忘录对象是一种原发器对象状态的表示,不会被其他代码所改动。备忘录保存了原发器的状态,采用列表、堆栈等集合来存储备忘录对象可以实现多次撤销操作。

缺点

  1. 资源消耗过大。如果需要保存的原发器类的成员变量太多,就不可避免地需要占用大量的存储空间,每保存一次对象的状态都需要消耗一定的系统资源。

适用场景

  1. 保存一个对象在某一个时刻的全部状态或部分状态,这样以后需要时就能够恢复到先前的状态,实现撤销操作。

  2. 防止外界对象破坏一个对象历史状态的封装性,避免将对象历史状态的实现细节暴露给外界对象。

练习

略(参考实现部分)

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
灏瀚星空6 分钟前
从基础到实战的量化交易全流程学习:1.2 金融市场基础
笔记·python·信息可视化·系统架构·开源
盐烟22 分钟前
C语言-函数练习1
c语言·开发语言·笔记
移知1 小时前
毫米波振荡器设计知识笔记
笔记
BOF_dcb1 小时前
数据结构强化篇
笔记
普普通通的一名码农1 小时前
电化学-论文分享-NanoStat: An open source, fully wireless potentiostat
笔记·开源
Fency咖啡2 小时前
《代码整洁之道》第7章 错误处理 - 笔记
笔记
Small踢倒coffee_氕氘氚2 小时前
DeepSeek:重构人类文明的智能引擎
经验分享·笔记·灌灌灌灌
孞㐑¥3 小时前
C++之异常
开发语言·c++·经验分享·笔记
ApeAssistant3 小时前
Spring + 设计模式 (二十) 行为型 - 中介者模式
spring·设计模式
ApeAssistant3 小时前
Spring + 设计模式 (十九) 行为型 - 访问者模式
spring·设计模式