Design Patterns -- Memento

The intent of the Memento Pattern is to provide storage and restoration of an object's state.  In Design Patterns(GoF) , the intent of the Memento Pattern is stated as "Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later". A memento is a tiny repository that saves an object's state. see UML:

<o:p></o:p>

<v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape o:spid="_x0000_i1025" id="Image19" type="#_x0000_t75" alt="" style="WIDTH: 331.8pt; HEIGHT: 117.6pt"><v:imagedata o:href="http://www.dofactory.com/Patterns/Diagrams/memento.gif" src="file:///C:\DOCUME~1\YONGHU~1.PAN\LOCALS~1\Temp\msohtml1\01\clip_image001.gif"></v:imagedata></v:shape><o:p></o:p>

some description:<o:p></o:p>

  • Memento  (Memento) <o:p></o:p>
    • stores internal state of the Originator object. <o:p></o:p>
    • protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker can only pass the memento to the other objects. And ideally, only the originator that produces the memento would be permitted to access the memento's internal state. <o:p></o:p>
  • Originator  (SalesProspect) <o:p></o:p>
    • creates a memento containing a snapshot of its current internal state. <o:p></o:p>
    • uses the memento to restore its internal state <o:p></o:p>
  • Caretaker  (Caretaker) <o:p></o:p>
    • is responsible for the memento's safekeeping <o:p></o:p>
    • never operates on or examines the contents of a memento. <o:p></o:p>

The object(memento) which is to be persisted across session(object seralization)  should implement Serializable interface and the some fields initialed from files(like images) can be decleared as transient to so that these fields needn't to be saved  when persistance.<o:p></o:p>

On the other hand, memento can be persisted using String. This is suitable for the context that data should be able to be modified outside the system.<o:p></o:p>

At last , the words below are referred:"<o:p></o:p>

Rules of thumb <o:p></o:p>

  • If you only need one Memento, combine the Originator and Caretaker into one object (Brown, 1998) . <o:p></o:p>
  • If you need many Mementos, store only incremental changes (Brown, 1998) . This will help to save space. <o:p></o:p>
  • Memento often used in conjunction with Command, Iterator and Singleton. <o:p></o:p>
  • Implementation of the Memento design pattern varies depending on the programming language. Implement the Originator as a friend class to the Memento in C++. Implement the Memento as an inner-class of the Originator in Java (Achtziger, 1999) . <o:p></o:p>

"<o:p></o:p>

ref: http://pages.cpsc.ucalgary.ca/~kristen/index.shtml<o:p></o:p>

<o:p> </o:p>

 

你可能感兴趣的:(F#,ext,Access,UML)