Statement: This series of blog reference "Big Talk design mode", author Geoscience.
with The prototype instance specifies the kind of object created and Copy These prototypes are created New the object. Prototype mode allows an object to create another customizable object without having to know any details of how to create it, by passing a prototype object to the object to be created. , the object to be created is created by requesting the prototype object to copy them themselves . The main problem is the creation of "some complex objects" , which often face drastic changes due to the change of requirements, but they have more stable and consistent interfaces.
in PHP, the class has implemented prototype mode, PHP has a magic Method __clone () method, will clone a such object.
Take a look at the UML class diagram:
Role Analysis:
1. Abstract prototype, provides a cloned interface
2. Specific prototypes to implement the cloned interface
The specific code:
/** Abstract Prototype Classes * Class Prototype */abstract class prototype{ abstract function cloned ();} /** Concrete Prototype class * Class Plane */class Plane extends prototype{public $color; function Fly () { echo "plane flies, flies!<br/>"; } function cloned () { return clone $this; }}
Client Test Code:
Header ("Content-type:text/html;charset=utf-8");//------------------------prototype mode test code------------------require_ Once "./prototype/prototype.php"; $plane 1=new Plane (); $plane 1->color= "Blue"; $plane 2= $plane 1->cloned (); $ Plane1->fly (); $plane 2->fly (); echo "Plane1 color: {$plane 1->color}<br/>"; echo "Plane2 color: {$plane 2- >color}<br/> ";
here is just a prototype model of the core idea, in fact, in the actual development of the direct clone can be.
PHP Object-oriented design pattern
PHP design mode-prototype mode