Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memento design pattern and State design pattern

Memento design pattern is used for restoring objects to previous state, like undo. But we can do undo and redo multiple times. I have read articles multiple times but still unclear to me as to are they similar or complement each other and can be used together.

Can State pattern be related to Memento Design pattern or be used together?

like image 415
Narendra Pathai Avatar asked Aug 24 '13 15:08

Narendra Pathai


People also ask

What is Memento design pattern used for?

Memento pattern is used to restore state of an object to a previous state. As your application is progressing, you may want to save checkpoints in your application and restore back to those checkpoints later. originator : the object for which the state is to be saved.

What is Memento design?

The memento pattern is a software design pattern that exposes the private internal state of an object. One example of how this can be used is to restore an object to its previous state (undo via rollback), another is versioning, another is custom serialization.

What is Memento design pattern in Java?

Memento is a behavioral design pattern that allows making snapshots of an object's state and restoring it in future. The Memento doesn't compromise the internal structure of the object it works with, as well as data kept inside the snapshots.

What is Memento in Sadp?

Stores internal state of the originator object. The state can include any number of state variables. The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus maintains the encapsulation.


3 Answers

The "state" in the "state pattern" is not the same kind of state that a memento pattern enables. A better name for the state pattern would be the "mode pattern". See this description of it for more details.

Now, one might use a memento to describe the state of an object that has a mode pattern involved, but that is the only particular relationship they have.

like image 104
tallseth Avatar answered Oct 08 '22 09:10

tallseth


The "state" in the Memento is the state you save for later retrieval. It's something like a bookmark. For example, you can save a video position and then return to that position using the Memento pattern. (States are saved in the Caretaker participant.)

The "states" in the State Design pattern are like those found in state machines (or state engines). They act like a larger context where each state has a finite set of moves. So if you're in the "On" state, your options are to stay in the "On" state or change to the "Off" state. (State patterns do not have conditional statements!)

For PHP examples of both see:

http://www.php5dp.com/category/design-patterns/memento/

and

http://www.php5dp.com/category/design-patterns/state/

like image 45
Bill Avatar answered Oct 08 '22 10:10

Bill


To answer your first question, you can use memento to redo by making a second memento instance to store the "state" of the redo. However, depending how complicated the object's state is to be stored, it is often "cheaper" to use the Command Pattern to provide undo/redo functionality. The Command can be use to store only the changes to undo/redo while memento probably needs to store the entire state.

tallseth's answer is also correct that the "state" from the State Pattern is not the same "state" that memento stores.

like image 2
dkatzel Avatar answered Oct 08 '22 09:10

dkatzel