原型模式

这个是原型模式的简单原理实现,还有一种是直接clone $class, 个人感觉没有封装,不太好,    还有继承一个抽象类的,原理是相同的

 

 1 <?php
 2 
 3 class Prototype
 4 {
 5     private $_num;
 6 
 7     public function getPrototype(){
 8         return clone $this;
 9     }
10 
11     public function setNum($num)
12     {
13         $this->_num = $num;
14     }
15 
16     public function getNum()
17     {
18         echo $this->_num,"<br/>";
19     }
20 }
21 
22 $a = new Prototype;
23 
24 $b = $a->getPrototype();
25 $b->setNum(3);
26 $b->getNum();
27 
28 $c = $a->getPrototype();
29 $c->setNum(5);
30 $c->getNum();
31 
32 
33 $b->getNum();
View Code

 

posted @ 2017-01-06 11:54  _logan  阅读(108)  评论(0编辑  收藏  举报