2015-05-29 12:11:16 +02:00
|
|
|
|
`Memento`__
|
|
|
|
|
===========
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
|
|
|
|
Purpose
|
|
|
|
|
-------
|
|
|
|
|
|
2015-05-30 00:04:55 +03:00
|
|
|
|
It provides the ability to restore an object to it's previous state (undo
|
2015-05-30 03:58:34 +03:00
|
|
|
|
via rollback) or to gain access to state of the object, without revealing
|
2016-09-22 10:39:03 +02:00
|
|
|
|
it's implementation (i.e., the object is not required to have a function
|
|
|
|
|
to return the current state).
|
2015-05-30 03:58:34 +03:00
|
|
|
|
|
|
|
|
|
The memento pattern is implemented with three objects: the Originator, a
|
|
|
|
|
Caretaker and a Memento.
|
|
|
|
|
|
|
|
|
|
Memento – an object that *contains a concrete unique snapshot of state* of
|
|
|
|
|
any object or resource: string, number, array, an instance of class and so on.
|
2015-05-30 00:04:55 +03:00
|
|
|
|
The uniqueness in this case does not imply the prohibition existence of similar
|
2015-05-30 03:58:34 +03:00
|
|
|
|
states in different snapshots. That means the state can be extracted as
|
|
|
|
|
the independent clone. Any object stored in the Memento should be
|
|
|
|
|
*a full copy of the original object rather than a reference* to the original
|
|
|
|
|
object. The Memento object is a "opaque object" (the object that no one can
|
|
|
|
|
or should change).
|
|
|
|
|
|
|
|
|
|
Originator – it is an object that contains the *actual state of an external
|
|
|
|
|
object is strictly specified type*. Originator is able to create a unique
|
|
|
|
|
copy of this state and return it wrapped in a Memento. The Originator does
|
|
|
|
|
not know the history of changes. You can set a concrete state to Originator
|
|
|
|
|
from the outside, which will be considered as actual. The Originator must
|
|
|
|
|
make sure that given state corresponds the allowed type of object. Originator
|
|
|
|
|
may (but not should) have any methods, but they *they can't make changes to
|
|
|
|
|
the saved object state*.
|
|
|
|
|
|
|
|
|
|
Caretaker *controls the states history*. He may make changes to an object;
|
|
|
|
|
take a decision to save the state of an external object in the Originator;
|
|
|
|
|
ask from the Originator snapshot of the current state; or set the Originator
|
|
|
|
|
state to equivalence with some snapshot from history.
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
|
|
|
|
|
- The seed of a pseudorandom number generator
|
|
|
|
|
- The state in a finite state machine
|
2015-05-30 03:58:34 +03:00
|
|
|
|
- Control for intermediate states of `ORM Model <http://en.wikipedia.org/wiki/Object-relational_mapping>`_ before saving
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
|
|
|
|
UML Diagram
|
|
|
|
|
-----------
|
|
|
|
|
|
|
|
|
|
.. image:: uml/uml.png
|
|
|
|
|
:alt: Alt Momento UML Diagram
|
|
|
|
|
:align: center
|
|
|
|
|
|
|
|
|
|
Code
|
|
|
|
|
----
|
|
|
|
|
|
2017-04-03 19:14:10 -06:00
|
|
|
|
You can also find this code on `GitHub`_
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
|
|
|
|
Memento.php
|
|
|
|
|
|
|
|
|
|
.. literalinclude:: Memento.php
|
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
2016-12-16 01:00:37 +03:00
|
|
|
|
State.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
2016-12-16 01:00:37 +03:00
|
|
|
|
.. literalinclude:: State.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
2016-09-22 10:39:03 +02:00
|
|
|
|
Ticket.php
|
2015-04-08 23:19:24 +02:00
|
|
|
|
|
2016-09-22 10:39:03 +02:00
|
|
|
|
.. literalinclude:: Ticket.php
|
2015-04-08 23:19:24 +02:00
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
2015-04-02 00:03:33 +02:00
|
|
|
|
Test
|
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
Tests/MementoTest.php
|
|
|
|
|
|
|
|
|
|
.. literalinclude:: Tests/MementoTest.php
|
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
2020-06-20 11:27:02 -03:00
|
|
|
|
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/main/Behavioral/Memento
|
2016-12-16 01:00:37 +03:00
|
|
|
|
.. __: http://en.wikipedia.org/wiki/Memento_pattern
|