search
Homephp教程php手册PHP设计模式原型模式

PHP设计模式原型模式

Jun 13, 2016 am 09:05 AM
prototypemodelDesign Patterns

PHP设计模式——原型模式

 

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。它主要面对的问题是:“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是他们却拥有比较稳定一致的接口。

 

在php中,类已经实现了原型模式,php有个魔术方法__clone()方法,会克隆出一个这样的对象。

看一下UML类图:

 

角色分析:

1.抽象原型,提供了一个克隆的接口

2.具体的原型,实现克隆的接口

 

具体的代码:

 

/**抽象原型类
 * Class Prototype
 */
abstract class Prototype
{
   abstract function cloned();
}

/**具体原型类
 * Class Plane
 */
class Plane extends Prototype
{

    public $color;

    function Fly()
    {
        echo 飞机飞啊飞!
;
    }

    function cloned()
    {
        return clone $this;
    }
}

 

客户端测试代码:

 

header(Content-Type:text/html;charset=utf-8);
//------------------------原型模式测试代码------------------
require_once ./Prototype/Prototype.php;

$plane1=new Plane();
$plane1->color=Blue;

$plane2=$plane1->cloned();

$plane1->Fly();
$plane2->Fly();

echo plane1的颜色为:{$plane1->color}
;
echo plane2的颜色为:{$plane2->color}
;

这里只是介绍一下原型模式的核心思想,其实在实际开发中直接clone即可。

 

 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.