当前位置:网站首页>行为型模式之备忘录模式
行为型模式之备忘录模式
2020-11-06 21:35:00 【程序猿欧文】
目录
- 定义与特点
- 结构与实现
- 模式的结构
- 模式的实现
- 应用场景
- 扩展:同原型模式混合使用
每个人都有犯错误的时候,都希望有种“后悔药”能弥补自己的过失,让自己重新开始,但现实是残酷的。在计算机应用中,客户同样会常常犯错误,能否提供“后悔药”给他们呢?当然是可以的,而且是有必要的。这个功能由“备忘录模式”来实现。
其实很多应用软件都提供了这项功能,如 Word、记事本、Photoshop、Eclipse 等软件在编辑时按 Ctrl+Z 组合键时能撤销当前操作,使文档恢复到之前的状态;还有在 IE 中的后退键、数据库事务管理中的回滚操作、玩游戏时的中间结果存档功能、数据库与操作系统的备份操作、棋类游戏中的悔棋功能等都属于这类。
备忘录模式能记录一个对象的内部状态,当用户后悔时能撤销当前操作,使数据恢复到它原先的状态。
定义与特点
备忘录(Memento)模式的定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态,该模式又叫快照模式。
备忘录模式是一种对象行为型模式,其主要优点如下:
- 提供了一种可以恢复状态的机制:当用户需要时能够比较方便地将数据恢复到某个历史的状态。
- 实现了内部状态的封装:除了创建它的发起人之外,其他对象都不能够访问这些状态信息。
- 简化了发起人类:发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。
其主要缺点是:资源消耗大,如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源。
结构与实现
备忘录模式的核心是设计备忘录类以及用于管理备忘录的管理者类,现在我们来学习其结构与实现。
模式的结构
备忘录模式的主要角色如下:
- 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。
- 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
- 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
备忘录模式的结构图如图所示:
模式的实现
备忘录模式的实现代码如下:
class Program{ static void Main(string[] args) { Originator or = new Originator(); Caretaker cr = new Caretaker(); or.SetState("S0"); Console.WriteLine("初始状态:" + or.GetState()); cr.SetMemento(or.CreateMemento()); //保存状态 or.SetState("S1"); Console.WriteLine("新的状态:" + or.GetState()); or.RestoreMemento(cr.GetMemento()); //恢复状态 Console.WriteLine("恢复状态:" + or.GetState()); Console.Read(); }}//备忘录public class Memento{ private String state; public Memento(String state) { this.state=s.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4553470
边栏推荐
- 零基础打造一款属于自己的网页搜索引擎
- 有了这个神器,快速告别垃圾短信邮件
- Discussion on the technical scheme of text de duplication (1)
- PHP application docking justswap special development kit【 JustSwap.PHP ]
- In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
- It's time for your financial report to change to a more advanced style -- financial analysis cockpit
- Introduction to Google software testing
- C + + and C + + programmers are about to be eliminated from the market
- Filecoin has completed a major upgrade and achieved four major project progress!
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
猜你喜欢
01. SSH Remote terminal and websocket of go language
Building and visualizing decision tree with Python
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
Use modelarts quickly, zero base white can also play AI!
Individual annual work summary and 2019 work plan (Internet)
For a while, a dynamic thread pool was created, and the source code was put into GitHub
Pattern matching: The gestalt approach一种序列的文本相似度方法
2020年数据库技术大会助力技术提升
Shh! Is this really good for asynchronous events?
PHP application docking justswap special development kit【 JustSwap.PHP ]
随机推荐
仅用六种字符来完成Hello World,你能做到吗?
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
一部完整的游戏,需要制作哪些音乐?
Individual annual work summary and 2019 work plan (Internet)
大道至简 html + js 实现最朴实的小游戏俄罗斯方块
(2) ASP.NET Core3.1 Ocelot routing
华为云微认证考试简介
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
Markdown tricks
代码生成器插件与Creator预制体文件解析
nacos、ribbon和feign的簡明教程
Introduction to quantitative investment and Trading (Python introduction to financial analysis)
Jmeter——ForEach Controller&Loop Controller
新建一个空文件占用多少磁盘空间?
Share with Lianyun: is IPFs / filecoin worth investing in?
有了这个神器,快速告别垃圾短信邮件
Construction of encoder decoder model with keras LSTM
Azure data factory (3) integrate azure Devops to realize CI / CD
How to turn data into assets? Attracting data scientists