簡介:這是php設計模式 Memento (備忘錄模式)的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=339497' scrolling='no'>
1 <?php
2 /**
3 * 備忘錄模式
4 *
5 * 在不破壞封裝性的前提下,捕獲一個對象的內部狀態,並在該對象之外儲存這個狀態,這樣以後就可以將該對象恢複到儲存的狀態
6 */
7 class Memento
8 {
9 private $_state = null;
10
11 public function __construct($state)
12 {
13 $this->_state = $state;
14 }
15
16 public function getState()
17 {
18 return $this->_state;
19 }
20 }
21
22 class Caretaker
23 {
24 private $_memento = null;
25
26 public function getMemento()
27 {
28 return $this->_memento;
29 }
30
31 public function setMemento($memento)
32 {
33 $this->_memento = $memento;
34 }
35 }
36
37 class Originator
38 {
39 private $_state = null;
40
41 public function getState()
42 {
43 return $this->_state;
44 }
45
46 public function setState($state)
47 {
48 $this->_state = $state;
49 }
50
51 public function createMemento()
52 {
53 return new Memento($this->_state);
54 }
55
56 public function setMemento($memento)
57 {
58 $this->_state = $memento->getState();
59 }
60
61 public function display()
62 {
63 echo "state = ".$this->_state."<br/>";
64 }
65 }
66
67 $objOriginator = new Originator();
68 $objOriginator->setState(0);
69 $objOriginator->display();
70
71 $objCareTaker = new CareTaker();
72 $objCareTaker->setMemento($objOriginator->createMemento());
73
74 $objOriginator->setState(1);
75 $objOriginator->display();
76
77 $objOriginator->setMemento($objCareTaker->getMemento());
78 $objOriginator->display();
愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具
http://biancheng.dnbcw.info/php/339497.html pageNo:8