The 17th chapter of the design pattern-Memo mode (Java implementation)
A good man is me, I am Zeng. The recent events of Chenhe and Zhang Zixian have been a raging uproar. Think of every year there are love apartments accompany me now the new Year without a love apartment always feel a lack of something. I do not know if you remember the love apartment in a classic bridge-every time Sekiya brewery and Tang leisurely quarrel, always can be "archive", first dry other things, and then have time to continue to "read the document", this is how good a skill ah, think about it, each quarrel, archive can do other things to calm down a bit, Then read the document when the time has been calm well, is not clear a lot of it, is not so many misunderstanding can not be lifted? In this case, two of them used the next "Memo mode". All right, here comes the memo ~ (everyone: Fish elder brother is more and more wordy ah.) Oh, no, no, it's a foodie. )
Self-introduction of Memo Mode
In my humble opinion, a memo model that does not exist in the real world is provided. What is said to be easy to understand is the legendary "regret medicine." Think about it, when you do something wrong, just "read the document" Before you can do it again, that's how many people think about it, or when you miss something for some reason, how exciting it is to "read the document" before it. In this case, how many do not have the courage to do things, will have "regret medicine" after the courage to try again and again, in the game, dare to countless times of the reclamation, PK people is not because of the infinite "resurrection." Well, no longer verbose, the definition for me is: without violating cncapsulation, capture and externalize an object ' s internal state so, the object can Is restored to the state later. The translation is to say: Capture the internal state of an object and save it outside the object without damaging the encapsulation. The object can then be restored to its previously saved state. To put it bluntly, it means to back up an object and provide a backup method of the program data, which is the General class diagram:
Self-analysis of memo mode
Analysis not moving, disadvantage first:
- First of all, the cost of using a memo is higher: If the generator has to copy and store a lot of information when it generates a memo, or if the customer creates a memo and restores the status of the generator very frequently, this can be a significant overhead.
- In some languages, it is difficult to guarantee that only the sender can access the state of the memo.
- The potential cost of maintaining a memo is high. Because the manager does not know how many states are in the memo, a very small manager can also generate a large amount of storage when storing the memo.
as advantages, listed as follows:
- Keep the encapsulation boundary. Use memos to avoid exposing information that should only be managed by the original and that must be stored outside of the original sender.
- simplifies the original generator. By handing over the burden of storage management to originator, allowing customers to manage the state of their requests will simplify originator and allow customers to end their work without notifying the sender.
Implementation of the memo mode
Since all speak of the love apartment, then the love of the apartment Guangulai to lift a chestnut. The first is the grain, the grain of the Sekiya brewery, the mood of the quarrel and the changes that Sekiya brewery after the archive:
1 Public classguangu{2 //the state of Sekiya brewery during a quarrel3 PrivateString state = "";4 //state changes after archiving5 Public voidchangestate () {6 This. state = "Mood becomes calm";7 }8 9 PublicString getState () {Ten returnState ; One } A - Public voidsetState (String state) { - This. State =State ; the } - - //Keep Backup - PublicMemento Creatememento () { + return NewMemento ( This. State); - } + //Restore Backup A Public voidRestorememento (Memento Memento) { at This. SetState (Memento.getstate ()); - } -}View Code
Next is the more important memento class, which is extremely simple to implement and very pure JavaBean:
1 Public classmemento{2 //the mood of Sekiya brewery3 PrivateString state = "";4 //passing a mood through a constructor function5 PublicMemento (String state) {6 This. State =State ;7 }8 9 PublicString getState () {Ten returnState ; One } A - Public voidsetState (String state) { - This. State =State ; the } -}
Finally, the Administrator role of the memo is used to manage, save, and provide memos. The Administrator class for memos is also simple:
1 Public classcaretaker{2 //Memo Object3 PrivateMemento Memento;4 PublicMemento Getmemento () {5 returnMemento;6 }7 Public voidSetmemento (Memento Memento) {8 This. Memento =Memento;9 }Ten}
At this point, the memo pattern is realized.
Application Scenarios of Memo mode
So the question is, when can I use the memo mode:
- Related state scenarios where data needs to be saved and restored.
- Provides an operation that can be rolled back, such as CTRL + Z key combinations in various editors.
- The replica scene that needs to be monitored.
- Transaction management for database connections is the memo pattern used.
Above. How to predict the post-style, and listen to tell.
PS: This blog welcome forwarding, but please specify the blog address and author ~
Blog Address: http://www.cnblogs.com/voidy/
Blog: http://voidy.net
<. ))) ≦
The 17th chapter of the design pattern-Memo mode (Java implementation)