Home > php教程 > php手册 > php设计模式 原型模式,设计模式原型

php设计模式 原型模式,设计模式原型

WBOY
Release: 2016-06-13 08:45:20
Original
961 people have browsed it

php设计模式 原型模式,设计模式原型

原型模式与工程模式作用类似,都是用来创建对象。

与工程模式的实现不同,原型模式是先创建好一个原型对象,然后铜鼓clone原型对象来创建新的对象。这样就免去了类创建时重复的原始化操作。

原型模式适用于大对象的创建。创建一个大对象需要很大的开销,如果每次new就会消耗很大,原型模式仅需内存拷贝即可。

index.php

<span>$prototype</span> = <span>new</span><span> Baobab\Canvas();
</span><span>$prototype</span>-><span>init();

</span><span>$canvas1</span> = <span>clone</span> <span>$prototype</span><span>;
</span><span>$canvas1</span>->rect(3,6,4,12<span>);
</span><span>$canvas1</span>-><span>draw();
</span><span>$canvas2</span> = <span>clone</span> <span>$prototype</span><span>;
</span><span>$canvas2</span>->rect(3,6,4,12<span>);
</span><span>$canvas2</span>->draw();
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template