Learn PHP Design Patterns PHP Implementation prototype (prototype) _php tips

Source: Internet
Author: User
Tags abstract learn php serialization shallow copy

I. Intention
Specify the type of object to create with a prototype instance, and create a new object by copying the prototypes
Second, the prototype pattern structure diagram

Third, the main role in the prototype model
Abstract prototype (Prototype) role: Declares an interface for cloning itself

Specific prototype (concrete Prototype) role: Implementing a clone itself

Iv. advantages and disadvantages of the prototype model
Advantages of prototype mode:
1. Can add and remove products at run time
2, you can change the value to specify a new object
3, you can change the structure to specify new objects
4, reduce the construction of subclasses
5, use class dynamic configuration Application

Disadvantages of the prototype model:
The main drawback of the prototype model is that each class must be equipped with a clone method.
And this cloning method requires a comprehensive consideration of the functionality of the class, which is not difficult for a new class, but it is not necessarily easy to transform an existing class.

Five, the prototype pattern applies the scene
1, when a system should be independent of its product creation, composition and representation, to use the prototype mode
2. When the class being instantiated is specified at run time, such as dynamic loading
3. To avoid creating a factory class level equal to the Product class level;
4. When an instance of a class can have only one of several different combinations of States. Creating a corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time with the appropriate state

VI. prototype model and other models

Abstract Factory mode (in abstract Factory mode): The abstract Factory mode and prototype mode are competing in some way, but they can also be used together.

Prototype Model PHP sample

<?php/** * Abstract prototype Role * * Interface Prototype {public function copy ();}
 
 /** * Specific Prototype role * * Class Concreteprototype implements prototype{private $_name;
 Public function __construct ($name) {$this->_name = $name;
 The Public Function SetName ($name) {$this->_name = $name;
 The Public Function GetName () {return $this->_name; The Public Function copy () {/* deep copy implementation $serialize _obj = serialize ($this);//serialization $clone _obj = Unserialize ($serialize _ob j);
 Reverse-serialization return $clone _obj; * * Return clone $this;
 
Shallow copy}}/** * Test deep copy of the reference class/class Demo {public $array;}
 Class Client {/** * Main program.
 * * public static function main () {$demo = new demo ();
 $demo->array = Array (1, 2);
 $object 1 = new Concreteprototype ($demo);
 
 $object 2 = $object 1->copy ();
 Var_dump ($object 1->getname ());
 echo ' <br/> ';
 Var_dump ($object 2->getname ());
 
 echo ' <br/> ';
 $demo->array = Array (3, 4);
 Var_dump ($object 1->getname ());echo ' <br/> ';
 Var_dump ($object 2->getname ());
 
 echo ' <br/> '; } client::main ();?>

add: shallow copy and deep copy

Shallow copy
All variables of the copied object contain the same values as the original object, and references to other objects still point to the original object.
That is, the shallow copy is responsible for only the current object instance and does not copy the referenced object.

Deep copy
All variables of the copied object contain the same values as the original object, except those that refer to other objects. Variables that refer to other objects will point to a new object that is being copied, not to the original referenced object.
That is, a deep copy copies all the objects referenced by the object to be copied, and this copy of the object being referred to is called an indirect copy.
Deep copy depth to how many layers, is an uncertain problem.
When deciding to copy an object in a deep copy, it is important to decide whether the object of the indirect copy is to take a shallow copy or a deep copy or continue with a deep copy.
Therefore, when taking a deep copy, you need to decide how deep it is. In addition, the problem of circular references is likely to occur in the process of deep copying.

Using serialization to make deep copies
Using serialization to make deep copies, the process of writing an object into a stream is a serialization (serilization) process, but in the industry the process of serializing this image is known as the "Frozen" or "pickled pickle" process. While the process of reading an object from a stream is called a deserialization (deserialization) process, also known as a "thaw" or "fresh" process.
Use the serialize and unserialize functions in PHP to implement serialization and deserialization.

The annotation in the above code is a process of serializing and deserializing the implementation of a deep copy.

The above is the use of PHP to implement the prototype model of the code, there are some ideas on the prototype model of the distinction, hoping to help you learn.

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.