当前位置:网站首页>The memorandum model of behavior model
The memorandum model of behavior model
2020-11-06 21:35:14 【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]所创,转载请带上原文链接,感谢
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World