当前位置:网站首页>行为型模式之备忘录模式
行为型模式之备忘录模式
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
边栏推荐
- Wow, elasticsearch multi field weight sorting can play like this
- Interface pressure test: installation, use and instruction of siege pressure test
- What knowledge do Python automated testing learn?
- Custom function form of pychar shortcut key
- 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
- 游戏开发中的新手引导与事件管理系统
- In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
- Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
- Interpretation of Cocos creator source code: engine start and main loop
猜你喜欢
Behind the first lane level navigation in the industry
Custom function form of pychar shortcut key
零基础打造一款属于自己的网页搜索引擎
检测证书过期脚本
What are the criteria for selecting a cluster server?
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
零基础打造一款属于自己的网页搜索引擎
How to hide part of barcode text in barcode generation software
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
一部完整的游戏,需要制作哪些音乐?
随机推荐
Building and visualizing decision tree with Python
(2) ASP.NET Core3.1 Ocelot routing
What are the common problems of DTU connection
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
华为云微认证考试简介
A brief history of neural networks
Xmppmini project details: step by step from the principle of practical XMPP technology development 4. String decoding secrets and message package
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
It is really necessary to build a distributed ID generation service
01. SSH Remote terminal and websocket of go language
事件监听问题
nacos、ribbon和feign的簡明教程
百万年薪,国内工作6年的前辈想和你分享这四点
Uncle Bob: the software architecture is similar to a house. Object oriented is the structure of the house, and the water pipe is functional programming
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
Analysis of serilog source code -- how to use it
Behind the first lane level navigation in the industry
Python基础变量类型——List浅析
有了这个神器,快速告别垃圾短信邮件
(1) ASP.NET Introduction to core3.1 Ocelot