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

介绍

备忘录模式在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。它是一种对象行为模式,别名为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. 防止外界对象破坏一个对象历史状态的封装性,避免将对象历史状态的实现细节暴露给外界对象。

练习

略(参考实现部分)

相关推荐
hssfscv1 小时前
Javaweb 学习笔记——html+css
前端·笔记·学习
Dream Algorithm3 小时前
自古英雄多寂寥
笔记
yuhaiqun19893 小时前
Typora 技能进阶:从会写 Markdown 到玩转配置 + 插件高效学习笔记
经验分享·笔记·python·学习·学习方法·ai编程·markdown
apcipot_rain3 小时前
汇编语言与逆向分析 一轮复习笔记
汇编·笔记·逆向
Lv11770084 小时前
Visual Studio中的多态
ide·笔记·c#·visual studio
HollowKnightZ4 小时前
论文阅读笔记:Class-Incremental Learning: A Survey
论文阅读·笔记
AA陈超4 小时前
枚举类 `ETriggerEvent`
开发语言·c++·笔记·学习·ue5
代码游侠5 小时前
学习笔记——IPC(进程间通信)
linux·运维·网络·笔记·学习·算法
apcipot_rain5 小时前
汇编语言程序设计 从0到1实战笔记
笔记
周小码5 小时前
我用一个周末,写了一个“反内卷“的极简笔记工具
笔记