-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy path21-memento.php
76 lines (60 loc) · 1.19 KB
/
21-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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
// === A Memento ===
// That store backup copy of a Car object
class Memento
{
private $car;
public function __construct(Car $car)
{
$this->car = $car;
}
public function getCar()
{
return $this->car;
}
}
class Car
{
public $color;
public function __construct($color)
{
$this->color = $color;
}
}
class Customizer
{
private $car;
public function __construct(Car $car)
{
$this->car = $car;
}
// Store backup copy in Memento
public function copy()
{
return new Memento( clone $this->car );
}
// Retrive backup from Memento
public function restore(Memento $memento)
{
$this->car = $memento->getCar();
}
public function changeColor($color)
{
$this->car->color = $color;
}
public function getColor()
{
return $this->car->color;
}
}
// ---
$custom = new Customizer( new Car("White") );
echo $custom->getColor() . "\n";
// Output: White
$backup = $custom->copy();
$custom->changeColor("Black");
echo $custom->getColor() . "\n";
// Output: Black
$custom->restore($backup);
echo $custom->getColor() . "\n";
// Output: White