介紹
? ? ? ? 備忘錄模式在不破壞封裝的前提下,捕獲一個對象的內(nèi)部狀態(tài),并在該對象之外保存這個狀態(tài),這樣可以在以后將對象恢復(fù)到原先保存的狀態(tài)。它是一種對象行為模式,別名為Token。
實現(xiàn)
myclass.h
//
// 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創(chuàng)建,確保安全
private:
Memento();
Memento(Originator *o);
void setState(const std::string &state);
std::string &getState();
private:
std::string m_state;
};
class Originator { // 原發(fā)器類,需要保存狀態(tài)
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 { // 負(fù)責(zé)人類,管理備忘錄
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
//
// 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
#include <iostream>
#include <mutex>
#include "myclass.h"
int main() {
Originator *originator = new Originator();
originator->setState("初始狀態(tài)");
auto m0 = originator->createMemento();
originator->setState("第一次狀態(tài)");
auto m1 = originator->createMemento();
originator->setState("第二次狀態(tài)");
auto m2 = originator->createMemento();
Caretaker *caretaker = new Caretaker();
std::cout << "恢復(fù)狀態(tài)前:" << std::endl;
std::cout << "\t" << originator->getState() << std::endl;
std::cout << "第一次恢復(fù):" << std::endl;
caretaker->setMemento(m2);
originator->restoreMemento(caretaker->getMemento());
std::cout << "\t" << originator->getState() << std::endl;
std::cout << "第二次恢復(fù):" << std::endl;
caretaker->setMemento(m1);
originator->restoreMemento(caretaker->getMemento());
std::cout << "\t" << originator->getState() << std::endl;
std::cout << "第三次恢復(fù):" << std::endl;
caretaker->setMemento(m0);
originator->restoreMemento(caretaker->getMemento());
std::cout << "\t" << originator->getState() << std::endl;
delete originator;
delete caretaker;
return 0;
}
總結(jié)
優(yōu)點
? ? ? ? 1.?它提供了一種狀態(tài)恢復(fù)的實現(xiàn)機(jī)制,使得用戶可以方便地回到一個特定的歷史步驟。當(dāng)新的狀態(tài)無效或者存在問題時,可以使用暫時存儲起來的備忘錄將狀態(tài)復(fù)原。
? ? ? ? 2.?備忘錄實現(xiàn)了對信息的封裝。一個備忘錄對象是一種原發(fā)器對象狀態(tài)的表示,不會被其他代碼所改動。備忘錄保存了原發(fā)器的狀態(tài),采用列表、堆棧等集合來存儲備忘錄對象可以實現(xiàn)多次撤銷操作。
缺點
? ? ? ? 1.?資源消耗過大。如果需要保存的原發(fā)器類的成員變量太多,就不可避免地需要占用大量的存儲空間,每保存一次對象的狀態(tài)都需要消耗一定的系統(tǒng)資源。
適用場景
? ? ? ? 1.?保存一個對象在某一個時刻的全部狀態(tài)或部分狀態(tài),這樣以后需要時就能夠恢復(fù)到先前的狀態(tài),實現(xiàn)撤銷操作。
? ? ? ? 2.?防止外界對象破壞一個對象歷史狀態(tài)的封裝性,避免將對象歷史狀態(tài)的實現(xiàn)細(xì)節(jié)暴露給外界對象。文章來源:http://www.zghlxwxcb.cn/news/detail-821362.html
練習(xí)
略(參考實現(xiàn)部分)文章來源地址http://www.zghlxwxcb.cn/news/detail-821362.html
到了這里,關(guān)于《設(shè)計模式的藝術(shù)》筆記 - 備忘錄模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!