php設計模式 Memento (備忘錄模式)

來源:互聯網
上載者:User

簡介:這是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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.