Definition: MementoPattern captures the internal state of an object without compromising encapsulation, and saves the State outside the object. In this way, the object can be restored to the previously saved state.
Type: object behavior mode. Overview: The usage of the memorandum mode is narrow, that is, the complex state needs to be restored to the previous state. In reality, only a very simple state needs to be restored. If you use the memorandum mode, you will feel a little useless. It's like using a cannon to fight mosquitoes. In Head First Design Patterns, the memorandum mode is also listed as one of the uncommon Patterns. The memorandum mode is relatively simple, that is, it abstracts the States that need to be saved in an object with a complex state and needs to be restored to Originator again, and provides some operation interfaces to encapsulate them into a Memento class. This greatly simplifies the complexity of the Originator class. Because Memento interfaces are in operational status, they should not be open to more people. Therefore, a Caretaker class managing Memento classes is encapsulated here, and provides some simple external interfaces. In fact, people who have played stand-alone drama games will immediately think of the "ARCHIVE" function. In a sense, the archiving function is an application of memos. Here is an example of my favorite game Warcraft III. Class diagram: Participants:
- Warcraft, or Originator, must be recorded and restored.
- Memento abstracts the internal status of Warcraft and provides operation methods.
- Caretaker manages Memento and provides methods for recording and restoring the status, but does not know the specific execution process.
Sample Code: Using System; using System. collections. generic; using System. text; namespace Pattern19 {class Warcrift {private string role; public string Role {get {return role;} set {role = value ;}} public Memento createmento () {return (new Memento (role);} public void SetMemento (Memento memento) {role = memento. role;} public void Start () {Console. writeLine ("There is a" + role) ;}} class Memento {private string role; public Memento (string _ role) {this. role = _ role;} public string Role {get {return role; }}} class Caretaker {private Memento memento; public Memento {get {return memento ;} set {memento = value ;}} class Program {static void Main (string [] args) {// The initial role is hero Warcrift war3 = new Warcrift (); war3.Role = "Hero"; war3.Start (); Caretaker caretaker = new Caretaker (); caretaker. memento = war3.CreateMemento (); // replace the role with Soldier war3.Role = "Soldier"; war3.Start (); // return to the initial state war3.SetMemento (caretaker. memento); war3.Start ();}}} Applicability:
- You must save the state of an object at a certain time point (partial) so that it can be restored to the previous state later.
- If an interface is used to directly obtain these States for other objects, the implementation details of the object will be exposed and the object encapsulation will be broken.
Advantages and disadvantages:
- It reduces the coupling between objects and the complexity of Object Management.
- Disadvantages: If you need to store a lot of information, you will be in favor of a very large overhead.
References:
- Design Patterns-reusable Object-Oriented Software basics
- Big talk Design Model
|