Design Mode Study Notes-Memorandum Mode
Before model learning
What is the design model: when we design the program, we gradually formed some typical problems and solutions, which is the software model; each mode describes a problem that often occurs in our program design and the solution to the problem. When we encounter the problem described by the mode, the corresponding solution can be used to solve the problem. This is the design mode.
A design pattern is an abstract thing. It is not learned but used. Maybe you don't know any pattern at all, and you don't consider any pattern, but write the best code, even from the perspective of "pattern experts", they are all the best designs and have to say "best pattern practices". This is because you have accumulated a lot of practical experience, knowing "where to write code" is a design pattern.
Some people say: "If the level is not reached, you can learn it in vain. If the level is reached, you can do it yourself ". It is true that the mode is well-developed and may still not be able to write good code, let alone design a good framework. OOP understanding and practical experience reach a certain level, and it also means to summarize a lot of good design experience, however, it may not be the case that "no teacher can do anything", or you can say that, on the basis of level and experience, it is time for the system to learn the "mode, it is helpful to learn the experts' summary and verify your own shortcomings.
This series of design patterns learning notes is actually a Learning Record for the book "Java and patterns.
Definition of memorandum Mode
Memento Pattern, also known as Snapshot Pattern or Token, is the behavior mode of an object.
A memorandum object is an object used to store snapshots of another object's internal state. The purpose of the memorandum mode is to capture the status of another object without damaging the encapsulation (enscapsolution) and store it externally (Externalize, this object can always be restored to the stored state when appropriate in the future.
Structure of memorandum Mode
Structure chart
Roles involved
(1) Memento role: will the Initiator? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/zo6zKudPDsbjN/keys/XtMyso7s8L3A + keys/keys + c1_vcd4kpha + keys + 0 s/keys /strong + strong/JxNzS0b6t1NrQ6NKqv + zV1bXEseCzzLOhvrDW0Mq508O5/dXi1tbEo8q9wcujrL/strong "Laugh" src = "http://www.bkjia.com/uploads/allimg/141021/04251Aa1-1.gif">
Code Implementation
Public class MementoTest {public static void main (String [] args) {Originator o = new Originator (); Caretaker c = new Caretaker (); o. setState ("On"); // change the initiator status to OnSystem. out. println ("Current State:" + o. getState (); c. setMemento (o. createMemento (); // store the snapshot o. setState ("Off"); // change the initiator status to OffSystem. out. println ("Current State:" + o. getState (); o. restoreMemento (c. getMemento (); // restore the System from the snapshot. out. println ("Current State:" + o. getState () ;}} class Memento {private String state; public Memento (String state) {this. state = state;} public String getState () {return state;} public void setState (String state) {this. state = state ;}} class Originator {private String state;/*** factory method, returns a new memo object */public Memento createMemento () {return new Memento (state);}/*** restores the initiator object to the state recorded by the memorandum object */public void restoreMemento (Memento m) {this. state = m. getState () ;}public String getState () {return state;} public void setState (String state) {this. state = state ;}} class Caretaker {private Memento memento; public Memento getMemento () {return memento;} public void setMemento (Memento memento) {this. memento = memento ;}}
Advantages and disadvantages of the memorandum Model
Advantages:
(1) Sometimes the status information of some initiator objects must be stored outside the initiator object, but must be read by the initiator object itself. In this case, the memorandum can shield the internal information of the initiator object from other objects, so that the encapsulated boundary can be properly maintained;
Advantages and disadvantages:
This mode simplifies the inator class. Of course, Memento and Caretaker classes are added at the same time. Is this an advantage or disadvantage?
We can only choose based on specific scenarios.
Applicable scenarios of memorandum Mode
(1) Support Technologies for stateless Http protocols, such as session and cookie;
(2) Some configuration files of Windows software are saved when they are closed, and the configuration files automatically reach the status when they are restarted.