[php]php设计模式 Prototype (原型模式)

1 <?php
2 /**
3 * 原型模式
4 *
5 * 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象
6 *
7 */
8 abstractclass Prototype
9 {
10 private$_id=null;
11
12 publicfunction __construct($id)
13 {
14 $this->_id =$id;
15 }
16
17 publicfunction getID()
18 {
19 return$this->_id;
20 }
21
22 publicfunction __clone() // magic function
23 {
24 $this->_id +=1;
25 }
26
27 publicfunction getClone()
28 {
29 returnclone$this;
30 }
31 }
32
33 class ConcretePrototype extends Prototype
34 {
35 }
36
37 //
38 $objPrototype=new ConcretePrototype(0);
39 $objPrototype1=clone$objPrototype;
40 echo$objPrototype1->getID()."<br/>";
41 $objPrototype2=$objPrototype;
42 echo$objPrototype2->getID()."<br/>";
43
44 $objPrototype3=$objPrototype->getClone();
45 echo$objPrototype3->getID()."<br/>";

posted on 2011-06-25 22:54  bluefrog  阅读(1385)  评论(0编辑  收藏  举报