PHP Design Pattern Learning Series (vii)--prototype object

Source: Internet
Author: User
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. the Prototype mode allows an object to recreate another customizable object without having to know any details about how to create it, by Pass a prototype object to the object to be created, the object that is being created is created by requesting the prototype object to copy 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:

[PHP] View plain copy print?

  1. /** Abstract Prototype class
  2. * Class Prototype
  3. */
  4. Abstract class Prototype
  5. {
  6. Abstract function cloned ();
  7. }
  8. /** Concrete Prototype class
  9. * Class Plane
  10. */
  11. class Plane extends Prototype
  12. {
  13. Public $color;
  14. function Fly ()
  15. {
  16. Echo "The plane flies, flies!"
    ";
  17. }
  18. function cloned ()
  19. {
  20. return Clone $this ;
  21. }
  22. }


Client Test Code:

[PHP] View plain copy print?

  1. Header ("Content-type:text/html;charset=utf-8");
  2. //------------------------prototype mode test code------------------
  3. require_once "./prototype/prototype.php";
  4. $plane 1 = New Plane ();
  5. $plane 1 ->color= "Blue" ;
  6. $plane 2 = $plane 1 ->cloned ();
  7. $plane 1 ->fly ();
  8. $plane 2 ->fly ();
  9. Echo "The color of the plane1 is: {$plane 1->color}
    ";
  10. Echo "The color of the Plane2 is: {$plane 2->color}
    ";


Here is just a prototype model of the core idea, in fact, in the actual development of the direct clone can be.

$plane 2=clone $plane 1;

$plane 2->fly ();

$plane 2->color;

The above introduces the PHP Design Pattern Learning Series (vii)-prototype object, including the content of the aspects, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.