php设计模式之原型模式(prototype)
发表于:2019-08-16 10:23:06浏览:58次
有时候,部分对象需要被初始化多次。而特别是在如果初始化需要耗费大量时间与资源的时候进行预初始化并且存储下这些对象,就会用到原型模式。
```php
_name = $name;
}
public function copy() {
return clone $this;
}
}
class Test { }
// client
$_prototype = new Prototype(new Test());
var_dump($_prototype);
// object(Prototype)#1 (1) { ["_name":"Prototype":private]=> object(Test)#2 (0) { } }
$_prototype_coty = $_prototype->copy();
var_dump($_prototype_coty);
// object(Prototype)#3 (1) { ["_name":"Prototype":private]=> object(Test)#2 (0) { } }
?>
```
输出
```php
vagrant@vagrant-ubuntu:~$ sudo php prototype.php
object(Prototype)#1 (1) {
["_name":"Prototype":private]=>
object(Test)#2 (0) {
}
}
object(Prototype)#3 (1) {
["_name":"Prototype":private]=>
object(Test)#2 (0) {
}
}
```

