This article mainly introduces the prototype mode in the php design mode and uses php to implement the prototype mode. if you are interested, refer
I. intention
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
II. Prototype pattern structure
III. main roles in prototype
Prototype role: declares an interface for cloning itself
Concrete Prototype role: to clone itself
IV. Advantages and disadvantages of the prototype
Prototype modeAdvantages:
1. you can add or delete products at runtime.
2. you can change the value to specify a new object.
3. you can change the structure to specify a new object.
4. reduce subclass construction
5. dynamically configure applications with categories
PrototypeDisadvantages:
The main disadvantage of the Prototype mode is that each class must have a clone method.
In addition, this cloning method requires a comprehensive consideration of the functions of the class, which is not very difficult for the brand-new class, but it is not always easy to transform the existing class.
V. application scenarios of the prototype model
1. when a system should be independent of its product creation, composition, and representation, Prototype mode should be used.
2. when the class to be instantiated is specified at the runtime, such as dynamic loading
3. to avoid creating a factory level equal to the product level;
4. When an instance of a class can only have one of several combinations of different states. Creating prototype numbers and cloning them may be more convenient than manually instantiating this type with the appropriate state each time.
VI. prototype mode and other modes
Abstract factory mode: Abstract Factory mode and Prototype mode compete with each other in some ways, but they can also be used together.
VII. prototype PHP example
<? 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;} public function setName ($ name) {$ this-> _ name = $ name ;} public function getName () {return $ this-> _ name;} public function copy () {/* deep copy implementation $ serialize_obj = serialize ($ this ); // serialize $ clone_obj = unserialize ($ serialize_obj); // deserialize return $ clone_obj; */return clone $ this; // shallow copy}/*** reference class for deep copy test */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'
'; Var_dump ($ object2-> getName (); echo'
'; $ Demo-> array = array (3, 4); var_dump ($ object1-> getName (); echo'
'; Var_dump ($ object2-> getName (); echo'
';}} Client: main () ;?>
Supplement:Shallow Copy and deep copy
Shortest copy
All the variables of the copied object contain the same value as the original object, and the reference to other objects still points to the original object.
That is, the shortest 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 value as the original object, except those variables that reference other objects. Variables that reference other objects will point to a new object to be copied, instead of the original referenced objects.
That is, the deep copy operation copies all the objects referenced by the objects to be copied. this kind of copy operation is called indirect copy.
How many layers do deep copy have to go deep.
When you decide to copy an object in the deep copy mode, you must decide whether to use the shortest copy or deep copy for the indirect copy object or continue to use the deep copy object.
Therefore, when deep copy is adopted, the depth must be determined to be deep. In addition, circular references may occur during deep copy.
Use serialization for deep copy
Using serialization for deep copy, the process of writing an object to a stream is a serialization process, however, in the industry, the serialization process is called "Frozen" or "pickles", while the process of reading an object from the stream is called Deserialization) the process, also known as the "unfreeze" or "restoration" process.
Use in PHPSerializeAndUnserialize functionImplement serialization and deserialization.
The annotation in the above code is a process of first serializing and then deserializing to achieve deep copy.
The above is the code that uses php to implement the prototype mode. there are some concepts about the prototype mode, and I hope it will be helpful for your learning.