Home  >  Article  >  Backend Development  >  Learn PHP design patterns PHP implements prototype mode (prototype)_php skills

Learn PHP design patterns PHP implements prototype mode (prototype)_php skills

WBOY
WBOYOriginal
2016-05-16 20:03:311088browse

1. Intention
Use prototype instances to specify the types of objects to create, and create new objects by copying these prototypes
2. Prototype mode structure diagram

3. Main characters in prototype mode
Abstract prototype (Prototype) role: declare an interface that clones itself

Concrete Prototype role: implement an operation of cloning itself

4. Advantages and Disadvantages of Prototype Mode
Prototype modeAdvantages:
1. Products can be added and deleted at runtime
2. You can change the value to specify a new object
3. The structure can be changed to specify new objects
4. Reduce the structure of subclasses
5. Use classes to dynamically configure applications

Disadvantages of Prototype mode
:
The main disadvantage of the Prototype pattern is that each class must be equipped with a clone method.

Moreover, this cloning method requires comprehensive consideration of the functions of the class. This is not difficult for a brand-new class, but it is not necessarily easy to transform an existing class.


5. Applicable Scenarios of Prototype Mode

1. When a system should be created, composed and represented independently of its products, use the Prototype pattern
2. When the class to be instantiated is specified at runtime, such as dynamic loading
3. In order to avoid creating a factory class hierarchy that is equal to the product class hierarchy;

4. When an instance of a class can only have one of several different state combinations. It may be more convenient to create a corresponding number of prototypes and clone them than to manually instantiate the class with the appropriate state each time

6. Prototype mode and other modes

Abstract factory mode: Abstract Factory mode and Prototype mode compete with each other in some aspects, but they can also be used together.

7. Prototype mode PHP example

<&#63;php
/**
 * 抽象原型角色
 */
interface Prototype {
 public function copy();
}
 
/**
 * 具体原型角色
 */
class ConcretePrototype implements Prototype{
 
 private $_name;
 
 public function __construct($name) {
 $this->_name = $name;
 }
 
 public function setName($name) {
 $this->_name = $name;
 }
 
 public function getName() {
 return $this->_name;
 }
 
 public function copy() {
 /* 深拷贝实现
 $serialize_obj = serialize($this); // 序列化
 $clone_obj = unserialize($serialize_obj); // 反序列化       
 return $clone_obj;
 */
 return clone $this; // 浅拷贝
 }
}
 
/**
 * 测试深拷贝用的引用类
 */
class Demo {
 public $array;
}
 
class Client {
 
 /**
 * Main program.
 */
 public static function main() {
 
 $demo = new Demo();
 $demo->array = array(1, 2);
 $object1 = new ConcretePrototype($demo);
 $object2 = $object1->copy();
 
 var_dump($object1->getName());
 echo '<br />';
 var_dump($object2->getName());
 echo '<br />';
 
 $demo->array = array(3, 4);
 var_dump($object1->getName());
 echo '<br />';
 var_dump($object2->getName());
 echo '<br />';
 
 }
 
}
 
Client::main();
&#63;>

Supplement:

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, shallow copy is only responsible for 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 variables that refer to other objects. Variables that reference other objects will point to a copied new object, not the original referenced objects.
That is, deep copy copies all the objects referenced by the object to be copied, and this copy of the referenced objects is called an indirect copy.
How deep a deep copy should go is an uncertain question.
When deciding to copy an object using a deep copy, you must decide whether to use a shallow copy, a deep copy, or continue to use a deep copy for the indirectly copied object.

Therefore, when taking a deep copy, you need to decide how deep is considered deep. In addition, during the deep copy process, circular reference problems are likely to occur.

Use serialization to make deep copies

Using serialization to make deep copies, the process of writing objects into the stream is the serialization process, but in the industry, the serialization process is also vividly called the "freezing" or "pickling" process; and The process of reading objects from the stream is called the deserialization process, also known as the "thawing" or "refreshing" process. Use the serialize and unserialize functions

to implement serialization and deserialization in PHP.

The comment in the above code is a process of serializing first and then deserializing to implement deep copy.

The above is the code to implement the prototype mode using PHP, and there are some conceptual distinctions about the prototype mode. I hope it will be helpful to everyone's learning.
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