php设计模式 Memento (备忘录模式)

简介:这是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

posted on 2011-11-12 09:08  圣者  阅读(144)  评论(0编辑  收藏  举报

导航