forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caretaker.php
33 lines (26 loc) · 912 Bytes
/
Caretaker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
namespace DesignPatterns\Behavioral\Memento;
class Caretaker
{
public static function run()
{
/* @var $savedStates Memento[] */
$savedStates = array();
$originator = new Originator();
//Setting state to State1
$originator->setState("State1");
//Setting state to State2
$originator->setState("State2");
//Saving State2 to Memento
$savedStates[] = $originator->saveToMemento();
//Setting state to State3
$originator->setState("State3");
// We can request multiple mementos, and choose which one to roll back to.
// Saving State3 to Memento
$savedStates[] = $originator->saveToMemento();
//Setting state to State4
$originator->setState("State4");
$originator->restoreFromMemento($savedStates[1]);
//State after restoring from Memento: State3
}
}