首页 > 代码库 > Prototype(原型模式)
Prototype(原型模式)
有些时候,部分对象需要被初始化多次。而特别是在如果初始化需要耗费大量时间与资源的时候进行预初始化并且存储下这些对象。
<?php interface Product {} class Factory { private $product; public function __construct(Product $product) { $this->product = $product; } public function getProduct() { return clone $this->product; }} class SomeProduct implements Product { public $name;} $prototypeFactory = new Factory(new SomeProduct()); $firstProduct = $prototypeFactory->getProduct();$firstProduct->name = ‘The first product‘; $secondProduct = $prototypeFactory->getProduct();$secondProduct->name = ‘Second product‘; print_r($firstProduct->name);// The first productprint_r($secondProduct->name);// Second product
Prototype(原型模式)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。