• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memento Vs Command pattern

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Memento pattern is aimed at using for "undo"ing actions (may be a set of actions/tasks/commands happened in a series).

Command pattern is aimed to decouple the tasks from the client besides providing a facility to undo an action (preferably last action).

How best we can demarcate between these two patterns? and how to make a choice between these two?

Can anyone throw some light on this? (It's little bit confusing to me at the "undo" part in both the patterns)

Thanks,
Uresh Kuruhuri
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Uresh Kuruhuri:
Command pattern is aimed to decouple the tasks from the client besides providing a facility to undo an action (preferably last action).



The command pattern doesn't necessarily have anything to do with an undo facility (though they like using them). A command is basically a single method wrapped up in an object together with its invocation parameters. Once the command is set up that method can be executed by anyone with access to the execute interface.

I think that Replace Conditional Dispatcher with Command shows one of the classic uses of the command pattern (and there is no undo facility involved).

The memento on the other hand simply stores information which reflects the current state of the originating class and it does so without revealing any of the state details to the caretaker which stores the memento.

Undo facilities which can simply undo by restoring the originator's previous state use mementos. In some systems however it isn't possible or feasible to store the entire state of the originator (which could be a complex composite). In that case the undo facility can use commands - whenever an action is received a compensating action is set up and stored in the undo facility. To undo you simply execute the commands in LIFO order until you undo all the necessary actions.

  • Undo with memento - undo by restoring a snapshot of the originator's state.
  • Undo with commands - undo by executing compensating actions.

  •  
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Great explanation!

    I'd like to add an example.

    For a GUI application, you may find there is a recent file list under File menu and also an Undo button in the menubar.

    The recent file list is a classic example for Memento pattern and the Undo button is a Command pattern.
     
    Uresh Kuruhuri
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I understand the point.

    Thanks Peer and Ben.

    -Uresh
     
    reply
      Bookmark Topic Watch Topic
    • New Topic