Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deeply understand the memo pattern of Java design pattern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to deeply understand the memo pattern of the Java design pattern, which is concise and easy to understand. I hope you can gain something through the detailed introduction of this article.

I. what is the memo mode

Definition: capture the internal state of an object without breaking the closure and save the state outside the object. This allows you to restore the object to its previously saved state later.

This mode is used to save the current state of the object and can be restored to that state again later. The memo pattern is implemented in a way that ensures that the state of the saved object cannot be accessed externally by the object

The purpose is to protect the integrity of the state of these saved objects and the internal implementation from being exposed to the outside.

II. The structure of the memorandum model

Roles are involved:

1.Originator (initiator): responsible for creating a memo Memento to record its own internal state at the current moment, and can use the memo to restore the internal state. Originator can decide which internal states Memento stores as needed.

2.Memento (memo): responsible for storing the internal state of Originator objects and preventing objects other than Originator from accessing memos. Memos have two interfaces: Caretaker can only see the narrow interface of memos, and he can only pass memos to other objects. Originator, on the other hand, can see the wide interface of the memo, allowing it to access all the data needed to return to the previous state.

3.Caretaker (manager): responsible for memo Memento, can not access or operate the content of Memento.

Third, the use scenario of the memo mode

1. The relevant state scenarios that need to save and restore the data.

two。 Provides a rollback (rollback) operation.

3. The transaction management of database connections is the memo mode used.

Fourth, the advantages and disadvantages of the memorandum model.

Advantages:

1. Sometimes the internal information of some initiator objects must be saved outside the initiator object, but it must be read by the initiator object itself. At this time, the memo mode can be used to shield the complex internal information of the initiator from other objects, so that the boundary of the encapsulation can be properly maintained.

2. This model simplifies the initiation of human beings. Sponsors no longer need to manage and save versions of their internal states, and clients can manage the versions of these states they need.

3. When the state of the initiator role changes, it is possible that the state is invalid, and the state can be restored using a temporarily stored memo.

Disadvantages:

1. If the state of the initiator role needs to be completely stored in the memo object, the memo object will be expensive in terms of resource consumption.

2. When the responsible person role stores a memo, the responsible person may not know how much storage space this state will take up, thus unable to remind the user whether an operation is expensive.

3. When the status of the initiator role changes, it is possible that the agreement is invalid. If the success rate of state change is not high, it is better to adopt the "if" protocol mode.

V. the realization of the memo model

Example: backup phone book

Contact-the data that needs to be backed up is status data and has no operation.

Public sealed class ContactPerson {/ / name public string Name {get; set;} / / phone number public string MobileNumber {get; set;}}

Sponsor-equivalent to [sponsor role] Originator

Public sealed class MobileBackOriginator {/ / Internal state that the initiator needs to save private List _ personList; public List ContactPersonList {get {return this._personList;} set {this._personList = value }} / / initialize the phone list that needs to be backed up public MobileBackOriginator (List personList) {if (personList! = null) {this._personList = personList;} else {throw new ArgumentNullException ("parameter cannot be empty!") Create a memo object instance and save the contact list to be saved in the memo object public ContactPersonMemento CreateMemento () {return new ContactPersonMemento (new List (this._personList));} / / restore the data backup in the memo to the contact list public void RestoreMemento (ContactPersonMemento memento) {this.ContactPersonList = memento.ContactPersonListBack } public void Show () {Console.WriteLine ("there are {0} individuals in the contact list, they are:", ContactPersonList.Count); foreach (ContactPerson p in ContactPersonList) {Console.WriteLine ("name: {0} number: {1}", p.Name, p.MobileNumber);}

Memo object, which is used to save state data, and saves the specific status data of the object at that time-- equivalent to [memo role] Memeto

Public sealed class ContactPersonMemento {/ / saves the phone list data created by the initiator, which is the so-called status public List ContactPersonListBack {get; private set;} public ContactPersonMemento (List personList) {ContactPersonListBack = personList;}}

Administrative role, which can manage [memo] objects. If you save multiple [memo] objects, of course, you can add, delete and other management processes to the saved objects-equivalent to [Manager role] Caretaker

Public sealed class MementoManager {/ / if you want to save multiple [memo] objects, you can save them through a dictionary or stack. Stack objects can reflect the order in which objects are saved / / for example: public Dictionary ContactPersonMementoDictionary {get; set;} public ContactPersonMemento ContactPersonMemento {get; set;}}

Client code

Class Program {static void Main (string [] args) {List persons = new List () {Name= "Huang Feihong", MobileNumber = "13533332222"}, new ContactPerson () {Name= "Fang Shiyu", MobileNumber = "13966554433"}, new ContactPerson () {Name= "Hong Xiguan", MobileNumber = "13198765544"} / / Mobile list initiator MobileBackOriginator mobileOriginator = new MobileBackOriginator (persons); mobileOriginator.Show (); / / create a memo and save the memo object MementoManager manager = new MementoManager (); manager.ContactPersonMemento = mobileOriginator.CreateMemento (); / / change the initiator contact list Console.WriteLine ("remove the last contact -") MobileOriginator.ContactPersonList.RemoveAt (2); mobileOriginator.Show (); / / restore to the original state Console.WriteLine ("- restore contact list -"); mobileOriginator.RestoreMemento (manager.ContactPersonMemento); mobileOriginator.Show (); Console.Read () }} the above is how to gain an in-depth understanding of the memo pattern of the Java design pattern. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report