当前位置:网站首页>The memorandum model of behavior model
The memorandum model of behavior model
2020-11-06 21:35:00 【Irving the procedural ape】
Catalog
- Definition and characteristics
- Structure and Implementation
- The structure of the pattern
- The realization of pattern
- Application scenarios
- Expand : Mixed with prototype patterns
Everyone makes mistakes sometimes , I hope there is a kind of “ Regret ” Make up for your own mistakes , Let yourself start over , But the reality is brutal . In computer applications , Customers also make mistakes often , Can you provide “ Regret ” Give it to them ? Of course you can , And it's necessary . This function consists of “ Memo mode ” To achieve .
In fact, many applications provide this function , Such as Word、 Notepad 、Photoshop、Eclipse Wait for the software to press when editing Ctrl+Z Key combination can undo the current operation , Restore the document to its previous state ; And in IE Back key in 、 Rollback operation in database transaction management 、 Save intermediate results when playing games 、 Database and operating system backup operation 、 The repentance function in chess games belongs to this kind of game .
Memo mode can record the internal state of an object , When the user regrets, the current operation can be undone , Restore data to its original state .
Definition and characteristics
Memorandum (Memento) Pattern The definition of : Without breaking encapsulation , Capture the internal state of an object , And save the state outside the object , So that the object can be restored to the original saved state when needed later , This mode is also called snapshot mode .
Memo mode is an object behavior pattern , Its main advantages are as follows :
- Provides a mechanism for restoring state : When the user needs to be able to more easily restore the data to a historical state .
- Realize the encapsulation of internal state : In addition to the founders who created it , No other object can access this state information .
- Simplified the originator : Initiators do not need to manage and save individual backups of their internal state , All status information is stored in the memo , And managed by the manager , This is in line with the principle of single responsibility .
Its main drawback is : High resource consumption , If the internal state information to be saved is too much or too frequent , It will occupy a large amount of memory resources .
Structure and Implementation
The core of the memo pattern is to design the memo class and the manager class for managing the memo , Now let's learn about its structure and implementation .
The structure of the pattern
The main roles of the memo model are as follows :
- Originator (Originator) role : Record the internal status information of the current time , Provides the ability to create and recover memo data , Implement other business functions , It has access to all the information in the memo .
- Memorandum (Memento) role : Responsible for storing the internal state of the initiator , Provide these internal states to the originator when needed .
- managers (Caretaker) role : Manage memos , Provides the ability to save and retrieve memos , But it cannot access and modify the contents of the memorandum .
The structure of the memo mode is shown in the figure :

The realization of pattern
The implementation code of memo mode is as follows :
class Program{ static void Main(string[] args) { Originator or = new Originator(); Caretaker cr = new Caretaker(); or.SetState("S0"); Console.WriteLine(" The initial state :" + or.GetState()); cr.SetMemento(or.CreateMemento()); // Save state or.SetState("S1"); Console.WriteLine(" New status :" + or.GetState()); or.RestoreMemento(cr.GetMemento()); // Restore the state Console.WriteLine(" Restore the state :" + or.GetState()); Console.Read(); }}// Memorandum public class Memento{ private String state; public Memento(String state) { this.state=s.........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
边栏推荐
- 2020-08-14:数据任务的执行引擎用的哪些?
- 2020-08-29:进程线程的区别,除了包含关系之外的一些区别,底层详细信息?
- How about small and medium-sized enterprises choose shared office?
- 意外的元素..所需元素..
- What is the purchasing supplier system? Solution of purchasing supplier management platform
- 迅为iMX6开发板-设备树内核-menuconfig的使用
- What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
- Hdu3974 assign the task segment tree DFS order
- An article taught you to use HTML5 SVG tags
- ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
猜你喜欢

Filecoin has completed a major upgrade and achieved four major project progress!

The isolation level of transaction and its problems

ado.net和asp.net的关系

STM32F030K6T6兼容替换灵动MM32F031K6T6

git远程库回退指定版本

The method of realizing high SLO on large scale kubernetes cluster

Can you do it with only six characters?

Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time

C and C / C + + mixed programming series 5 - GC collaboration of memory management

Common syntax corresponding table of mongodb and SQL
随机推荐
Git rebase is in trouble. What to do? Waiting line
MRAM高速缓存的组成
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
What are the highlights of Huawei mate 40 series with HMS?
Countdown | 2020 PostgreSQL Asia Conference - agenda arrangement of Chinese sub Forum
The role of theme music in games
上海巨微专用蓝牙广播芯片
面试官: ShardingSphere 学一下吧
Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
迅为iMX6开发板-设备树内核-menuconfig的使用
Introduction to Huawei cloud micro certification examination
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
mongo 用户权限 登录指令
Flink's datasource Trilogy: direct API
轻量型 GPU 应用首选 京东智联云推出 NVIDIA vGPU 实例
Python basic variable type -- list analysis
细数软件工程----各阶段必不可少的那些图
list转换map(根据key来拆分list,相同key的value为一个list)
Pn8162 20W PD fast charging chip, PD fast charging charger scheme
Common syntax corresponding table of mongodb and SQL