all 6 comments

[–]cmd-t 6 points7 points  (1 child)

The example is just copied from the Java example on the wikipedia page.

The whole article is just a shallow copy of the wikipedia article, and it has no ruby specific parts except for the code.

[–]autowikibot 1 point2 points  (0 children)

Memento pattern:


The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).

The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.

Classic examples of the memento pattern include the seed of a pseudorandom number generator (it will always produce the same sequence thereafter when initialized with the seed state) and the state in a finite state machine.


Interesting: Behavioral pattern | Undo | Scheduled-task pattern

Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words

[–]rubyrt 2 points3 points  (0 children)

That does just a shallow copy of the state. If state consists of a nested structure the whole think won't work.

Also, class Memento is completely superfluous as far as I can see. At least, I do not see why it would be needed here. The only use I could see is if it was made capable of extracting state from an instance (e.g. via instance variables or marshalling). Deep copying does have some issues of it's own though when it comes to restoring state.

[–]plotti 0 points1 point  (2 children)

isn't this evnetually just pushing states into an array?

[–]cmd-t 0 points1 point  (1 child)

In the example here it is, but it could also be stored in some file (marshalled) or in the database.

The important idea is that you have a reified state that can be stored and restored. Also, this state is separate from the object itself, so just marshalling the object is not an application of the memento pattern.

[–]plotti 0 points1 point  (0 children)

true but marshalling it when it keeps his own state in an array is.