-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemento.php
54 lines (52 loc) · 1.61 KB
/
Memento.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
class Originator { // Originator role
private $_state;
public function __construct() {
$this->_state = '';
}
public function createMemento() { // Create a memo
return new Memento($this->_state);
}
public function restoreMemento(Memento $memento) { // Restore the initiator to the state of the memo object record
$this->_state = $memento->getState();
}
public function setState($state) { $this->_state = $state; }
public function getState() { return $this->_state; }
public function showState() {
echo $this->_state;echo "<br>";
}
}
class Memento { // Memento role
private $_state;
public function __construct($state) {
$this->setState($state);
}
public function getState() { return $this->_state; }
public function setState($state) { $this->_state = $state;}
}
class Caretaker { // Caretaker role
private $_memento;
public function getMemento() { return $this->_memento; }
public function setMemento(Memento $memento) { $this->_memento = $memento; }
}
// client
/* Create target object */
$org = new Originator();
$org->setState('open');
$org->showState();
/* Create a note */
$memento = $org->createMemento();
/* Save this note with Caretaker */
$caretaker = new Caretaker();
$caretaker->setMemento($memento);
/* Change the state of the target object */
$org->setState('close');
$org->showState();
$org->restoreMemento($memento);
$org->showState();
/* Change the state of the target object */
$org->setState('close');
$org->showState();
/* Restore operation */
$org->restoreMemento($caretaker->getMemento());
$org->showState();