aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Memento Vs Command pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Memento Vs Command pattern" Watch "Memento Vs Command pattern" New topic
Author

Memento Vs Command pattern

Uresh Kuruhuri
Greenhorn

Joined: Nov 19, 2007
Posts: 2
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
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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.

  • Ben Two
    Ranch Hand

    Joined: Jul 06, 2007
    Posts: 35
    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

    Joined: Nov 19, 2007
    Posts: 2
    I understand the point.

    Thanks Peer and Ben.

    -Uresh
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Memento Vs Command pattern