Home > Backend Development > PHP Tutorial > PHP design pattern learning series (7)--Prototype objects

PHP design pattern learning series (7)--Prototype objects

WBOY
Release: 2016-07-29 08:57:27
Original
842 people have browsed it

Statement: The reference material for this series of blogs is "Dahua Design Pattern", written by Cheng Jie.

Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes. Prototype mode allows an object to create another customizable object without knowing any details of how to create it . By passing a prototype object to the object to be created, this object is to be created. Objects are created by requesting a copy of the prototype object . The main problem it faces is the creation of "some objects with complex structures"; due to changes in requirements, these objects often face drastic changes, but they have relatively stable and consistent interfaces. H p In PHP, the class has implemented the prototype mode. PHP has a magic method __Clone () method, which will cloning a object. Take a look at the UML class diagram:


Role analysis:

1. Abstract prototype provides a cloned interface

PHP design pattern learning series (7)--Prototype objects 2. Specific prototype and interface to implement cloning

Specific code:

[php] view plain

copy print

?

/**Abstract prototype classPHP design pattern learning series (7)--Prototype objectsPHP design pattern learning series (7)--Prototype objects

* Class Prototype
  1. */
  2. abstract
  3. class Prototype
  4. { abstract
  5. function
  6. cloned();
  7. }
  8. /**Specific prototype class
  9. * Class Plane
  10. */
  11. class
  12. Plane extends Prototype
  13. {
  14. public
  15. $ color
  16. ;
  17. function Fly()
  18.                                                                                                                                          }
  19. function
  20. cloned()
  21.                                         }
  22. Client test code:
  23.                                                                                plain copy
  24. print?
    1. header("Content-Type:text/html;charset=utf-8");
    2. //---------------- ----------Prototype mode test code------------------
    3. require_once "./Prototype/Prototype .php"; ->color=
    4. "Blue" ;
    5. $plane2=
    6. $plane1->cloned();
    7. $plane1->Fly( ); >color}"
    8. ; echo "The color of plane2 is: {$plane2->color}
      "
    9. ;
    10. Here is just the core idea of ​​the prototype mode. In fact, you can directly clone in actual development.
    11. $plane2=clone $plane1;
    12. $plane2->Fly();
    13. $plane2->color;
    14. The above has introduced the PHP design pattern learning series (seven) - prototype objects, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template