In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Original address: the memo pattern of toss Java design pattern
Memo mode
Without violating encapsulation, capture and externalize an object's internal state allowing the object to be restored to this state later.
Without breaking the encapsulation, capture the internal state of an object and save that state outside the object.
The so-called memo mode is to capture the internal state of an object without breaking the encapsulation, and save the state outside the object, so that the object can be restored to the previously saved state later. In many cases, we always need to record the internal state of an object. the purpose of this is to allow the user to cancel uncertain or erroneous operations, return to his original state, and give him "medicine for regret". The object state is stored specifically through a memo class. The customer is not coupled to the memo class, but to the memo management class.
Memo mode UML
Cdn.xitu.io/2019/4/9/169ffaebd9654dda?w=680&h=240&f=jpeg&s=26778 ">
UML class diagram
The Caretaker class is the Originator class used to save (createMemento ()) and restore (restore (memento)) the internal state of the initiator.
Initiator class implementation
(1) createMemento (), by creating and returning a memento object that stores the current internal state of the initiator
(2) restore (memento) by restoring the state from the incoming memento object.
UML sequence diagram
(1) Save the initiator's internal state: Caretaker calls createMemento () on Originator, creates a memento object, saves its current internal state (setState ()), and returns memento to Caretaker.
(2) restore the initiator's internal state: Caretaker calls restore (memento) on the Originator and specifies the memento object that stores the state to be restored. The initiator gets the status (getState ()) from the memento to set its own state.
Memo model role structure
(1) Memento role: the memo role stores the internal state of the memo initiating role. The memo initiating role determines which internal state of the memo role stores the memo initiating role as needed. To prevent objects other than the initiating role of the memo from accessing the memo. Memos actually have two interfaces, and the memo manager role can only see the narrow interface provided by the memo-it is not visible to the attributes stored in the memo role. The memo initiating role can see a wide interface that can get attributes that you put into the memo role.
(2) memo Originator role: the memo initiator role creates a memo to record its internal status at the current moment. Use memos to restore internal status as needed.
(3) the role of memo manager (Caretaker): responsible for keeping the memo. The contents of the memo cannot be operated or checked.
Examples of practical information
Source code address
Public class Caretaker { public static void main (String [] args) { List savedStates = new ArrayList (); Originator originator = new Originator (); originator.set ("State1"); originator.set ("State2"); savedStates.add (originator.saveToMemento ()); originator.set ("State3"); savedStates.add (originator.saveToMemento ()) originator.set ("State4"); originator.restoreFromMemento (savedStates.get (1)); }} @ Slf4jpublic class Originator { private String state; / / status change public void set (String state) { this.state = state; log.info ("Originator: Setting state to {}", state) } / / Save the status to the memo public Memento saveToMemento () { log.info ("Originator: Saving to Memento."); return new Memento (this.state); } / / take the status from the memo and roll back public void restoreFromMemento (Memento memento) { this.state = memento.getState () log.info ("Originator: State after restoring from Memento: {}", state); }} @ Data@AllArgsConstructorpublic class Memento { / / status maintenance private String state;}
Sample result
As you can see from the above code, as the status changes, use List to maintain the initiator's status list and take the status out of the memo in order to roll back the status.
Use in java
Generates a snapshot of the state of the object so that the object can restore its original state without exposing its own contents. The Date object implements the memo pattern through a long value within itself.
Java.util.Date
Java.io.Serializable
Summarize the advantages
1. Provide the user with a mechanism that can restore the state, so that the user can easily return to a certain historical state. 2. The encapsulation of the information is realized, so that the user does not need to care about the details of the state.
Shortcoming
Consume resources. If the class has too many member variables, it is bound to take up a lot of resources, and each save will consume a certain amount of memory.
Working with scen
1. Relevant state scenarios where you need to save / restore data. 2. Provide a rollback operation.
Matters needing attention
1. In order to comply with the Dimitt principle, a class for managing memos should be added. 2. To save memory, prototype mode + memo mode can be used.
Referenc
Memo mode | Rookie tutorial
Memento pattern
Count the design patterns in JDK
Welcome to follow us.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.