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/>";

相关文章:

  • 2021-08-09
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-07-04
  • 2021-09-24
  • 2022-03-10
猜你喜欢
  • 2021-11-30
  • 2022-01-20
  • 2022-12-23
  • 2021-11-20
  • 2021-12-31
  • 2021-10-16
  • 2021-06-13
相关资源
相似解决方案