听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • Python

  • Go

  • 数据结构与算法

  • 设计模式

    • 【设计模式】1 引言
    • 创建型

    • 结构型

    • 行为型

      • 【设计模式】行为型模式-Chain of Responsibility模式
      • 【设计模式】行为型模式-Command模式
      • 【设计模式】行为型模式-Interpreter模式
      • 【设计模式】行为型模式-Iterator模式
      • 【设计模式】行为型模式-Mediator模式
      • 【设计模式】行为型模式-Memento模式
      • 【设计模式】行为型模式-Observer模式
      • 【设计模式】行为型模式-State模式
      • 【设计模式】行为型模式-Strategy模式
      • 【设计模式】行为型模式-Template模式
      • 【设计模式】行为型模式-Visitor模式
  • 程序设计层
  • 设计模式
  • 行为型
zfprotectors
2022-05-18

【设计模式】行为型模式-Memento模式

# 功能

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态

# 解决

  • 主要解决:所谓备忘录模式就是在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态
  • 何时使用:很多时候我们总是需要记录一个对象的内部状态,这样做的目的就是为了允许用户取消不确定或者错误的操作,能够恢复到他原先的状态,使得他有"后悔药"可吃。
  • 如何解决:通过一个备忘录类专门存储对象状态
  • 关键代码:客户不与备忘录类耦合,与备忘录管理类耦合

# 优缺点

  • 优点:
    • 给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态
    • 实现了信息的封装,使得用户不需要关心状态的保存细节
  • 缺点:消耗资源。

# 应用场景

  • 应用实例:打游戏时的存档
  • 使用场景:
    • 需要保存/恢复数据的相关状态场景
    • 提供一个可回滚的操作
  • 注意事项:
    • 为了符合迪米特原则,还要增加一个管理备忘录的类
    • 为了节约内存,可使用原型模式+备忘录模式

# 简单示例代码部分

Memento.hpp

#ifndef _MEMENTO_H_ 
#define _MEMENTO_H_ 
#include <string>
#include <iostream>
using namespace std; 
class Memento;

class Originator{
public:
	typedef string State; 
	Originator(){
		_sdt="";
		_mt=0;
	}
	Originator(const State& sdt){
		_sdt=sdt;
		_mt=0;
	}
	~Originator(){}
	Memento* CreateMemento();
	void SetMemento(Memento* men){}
	void RestoreToMemento(Memento* mt);
	State GetState(){
		return _sdt;
	}
	void SetState(const State& sdt){
		_sdt=sdt;
	}
	void PrintState(){
		cout<<this->_sdt<<"....."<<endl;
	}
private:
	State _sdt;
	Memento* _mt; 
};

class Memento {
private:
	//这是最关键的地方,将 Originator 为 friend类,可以访问内部信息,但是其他类不能访问
	friend class Originator; 
	typedef string State; 
	Memento(){}
	Memento(const State& sdt){
		_sdt=sdt;
	} 
	~Memento(){}
	void SetState(const State& sdt){
		_sdt=sdt;
	}
	State GetState(){
		return _sdt;
	}
private:
	State _sdt; 
};

void Originator::RestoreToMemento(Memento* mt){
	this->_sdt=mt->GetState();
}
Memento* Originator::CreateMemento(){
	return new Memento(_sdt);
}
#endif 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

main.cpp

#include "Memento.h" 
#include <iostream>
using namespace std;
int main() {
	Originator* o = new Originator();
	o->SetState("old"); //备忘前状态 
	o->PrintState();
	Memento* m = o->CreateMemento(); //将状态备忘
	o->SetState("new"); //修改状态 
	o->PrintState(); 
	o->RestoreToMemento(m); //恢复修改前状态
	o->PrintState();
	return 0; 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
编辑 (opens new window)
#设计模式
上次更新: 2022/05/18, 15:47:37
【设计模式】行为型模式-Mediator模式
【设计模式】行为型模式-Observer模式

← 【设计模式】行为型模式-Mediator模式 【设计模式】行为型模式-Observer模式→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式