Memo Mode of design mode

Source: Internet
Author: User

In general, design patterns fall into three broad categories:
Create Patterns 5: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.
Structure Mode 7 types: Adapter mode, adorner mode, proxy mode, appearance mode, bridge mode, combined mode, and enjoy meta mode.
There are 11 types of behavioral Patterns: Policy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

Memo ModeThat is, "backup-Restore". implemented by two objects: originator and caretaker. The originator class represents the state in which its state can be stored and used to restore the state of an object using an inner class. This inner class is called a memo. This class is private and cannot be accessed by other objects. Caretaker is a helper class whose job is to help originator store the current state or restore its previous state through a memo. The memo is a private class of originator, and caretaker cannot access it, so it is stored as an object in caretaker

Demo: Text editor case

First, create the originator class
Public class filewriterutil {PrivateStringFileName; PrivateStringBuilderContent PublicFilewriterutil(Stringfile) {filename=file; Content=newStringBuilder(); }@OverridePublicStringToString () {returnContent.tostring (); } public void Write (StringSTR) {content.append (str); } publicMementoSave () {returnNewMemento(filename,content); } public void Undotolastsave (Objectobj) {MementoMemento = (Memento) obj;        Filename= Memento.filename;    Content=memento.content; } private class Memento{PrivateStringFileName; PrivateStringBuilderContent PublicMemento(StringFileStringBuilderContent) {this.filename=file; This.content=newStringBuilder(content);//Deep copy to prevent content from pointing to the same address}}}
Second, create the caretaker class
class FileWriterCaretaker {    Object obj;    public void save(FileWriterUtil fileWriter){        this.obj=fileWriter.save();    }    public void undo(FileWriterUtil fileWriter){        fileWriter.undoToLastSave(obj);    }}
then, create a test class
public class  filewriterclient  { public static void main (string  [] args) {filewritercaretaker  caretaker = new filewritercaretaker  (); filewriterutil  fileWriter = new filewriterutil  ( "info.txt" ); Filewriter.write ( "Remember to eat breakfast ~" ); system . Out.println (FileWriter); Caretaker.save (FileWriter); Filewriter.write ( "go out and remember to bring the key ~" ); system . Out.println (FileWriter); Caretaker.undo (FileWriter); system . Out.println (FileWriter); }}
Finally, check out the results


Remember to eat breakfast ~
Remember to eat breakfast ~ go out and remember to bring a key ~
Remember to eat breakfast ~

watch it.The ① memo class can only be accessed by originator objects. In the client program, use the caretaker object to finish saving or restoring the state of the originator object. ②originator objects have some properties that are not immutable, and we need to use deep copies or clones to avoid data integrity issues. Using serialization to get the implementation of a memo pattern is a common method, rather than creating a self-fulfilling memo for each object. ③ If the originator object is very large, the size of the memo object will be increased accordingly, thus requiring more memory space.

Memo Mode of design mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.