File tree Expand file tree Collapse file tree 6 files changed +146
-0
lines changed Expand file tree Collapse file tree 6 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Memento ;
4
+
5
+ class Caretaker
6
+ {
7
+ public static function run ()
8
+ {
9
+ /* @var $savedStates Memento[] */
10
+
11
+ $ savedStates = array ();
12
+
13
+ $ originator = new Originator ();
14
+
15
+ //Setting state to State1
16
+ $ originator ->setState ("State1 " );
17
+ //Setting state to State2
18
+ $ originator ->setState ("State2 " );
19
+ //Saving State2 to Memento
20
+ $ savedStates [] = $ originator ->saveToMemento ();
21
+ //Setting state to State3
22
+ $ originator ->setState ("State3 " );
23
+
24
+ // We can request multiple mementos, and choose which one to roll back to.
25
+ // Saving State3 to Memento
26
+ $ savedStates [] = $ originator ->saveToMemento ();
27
+ //Setting state to State4
28
+ $ originator ->setState ("State4 " );
29
+
30
+ $ originator ->restoreFromMemento ($ savedStates [1 ]);
31
+ //State after restoring from Memento: State3
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Memento ;
4
+
5
+ class Memento
6
+ {
7
+ /* @var string */
8
+ private $ state ;
9
+
10
+ /**
11
+ * @param string $stateToSave
12
+ */
13
+ public function __construct ($ stateToSave )
14
+ {
15
+ $ this ->state = $ stateToSave ;
16
+ }
17
+
18
+ /**
19
+ * @return string
20
+ */
21
+ public function getState ()
22
+ {
23
+ return $ this ->state ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Memento ;
4
+
5
+ class Originator
6
+ {
7
+ /* @var string */
8
+ private $ state ;
9
+
10
+ // The class could also contain additional data that is not part of the
11
+ // state saved in the memento..
12
+
13
+ /**
14
+ * @param string $state
15
+ */
16
+ public function setState ($ state )
17
+ {
18
+ $ this ->state = $ state ;
19
+ }
20
+
21
+ /**
22
+ * @return Memento
23
+ */
24
+ public function saveToMemento ()
25
+ {
26
+ return new Memento ($ this ->state );
27
+ }
28
+
29
+ public function restoreFromMemento (Memento $ memento )
30
+ {
31
+ $ this ->state = $ memento ->getState ();
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ # Memento
2
+
3
+ ## Purpose
4
+
5
+ Provide the ability to restore an object to its previous state (undo via rollback).
6
+
7
+ The memento pattern is implemented with three objects: the originator, a caretaker and a memento.
8
+ The originator is some object that has an internal state.
9
+ The caretaker is going to do something to the originator, but wants to be able to undo the change.
10
+ The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do.
11
+ To roll back to the state before the operations, it returns the memento object to the originator.
12
+ The memento object itself is an opaque object (one which the caretaker cannot, or should not, change).
13
+ 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.
14
+
15
+ ## Examples
16
+
17
+ * The seed of a pseudorandom number generator
18
+ * The state in a finite state machine
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ The patterns can be structured in roughly three different categories. Please cli
40
40
* [ Command] ( Command ) [ :notebook : ] ( http://en.wikipedia.org/wiki/Command_pattern )
41
41
* [ Iterator] ( Iterator ) [ :notebook : ] ( http://en.wikipedia.org/wiki/Iterator_pattern )
42
42
* [ Mediator] ( Mediator ) [ :notebook : ] ( http://en.wikipedia.org/wiki/Mediator_pattern )
43
+ * [ Memento] ( Memento ) [ :notebook : ] ( http://http://en.wikipedia.org/wiki/Memento_pattern )
43
44
* [ NullObject] ( NullObject ) [ :notebook : ] ( http://en.wikipedia.org/wiki/Null_Object_pattern )
44
45
* [ Observer] ( Observer ) [ :notebook : ] ( http://en.wikipedia.org/wiki/Observer_pattern )
45
46
* [ Specification] ( Specification ) [ :notebook : ] ( http://en.wikipedia.org/wiki/Specification_pattern )
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Tests \Memento ;
4
+
5
+ use DesignPatterns \Memento ;
6
+
7
+ /**
8
+ * MementoTest tests memento design pattern
9
+ */
10
+ class MementoTest extends \PHPUnit_Framework_TestCase
11
+ {
12
+
13
+ public function testOriginator ()
14
+ {
15
+ $ originator = new Memento \Originator ();
16
+ $ originator ->setState ("State1 " );
17
+
18
+ $ this ->assertAttributeEquals ("State1 " , "state " , $ originator );
19
+
20
+ $ originator ->setState ("State2 " );
21
+
22
+ $ this ->assertAttributeEquals ("State2 " , "state " , $ originator );
23
+
24
+ $ savedState = $ originator ->saveToMemento ();
25
+
26
+ $ this ->assertAttributeEquals ("State2 " , "state " , $ savedState );
27
+
28
+ $ originator ->setState ("State3 " );
29
+
30
+ $ this ->assertAttributeEquals ("State3 " , "state " , $ originator );
31
+
32
+ $ originator ->restoreFromMemento ($ savedState );
33
+
34
+ $ this ->assertAttributeEquals ("State2 " , "state " , $ originator );
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments