Use
prototype mode (Prototype) uses a prototype instance to specify the kind of object to create, and creates a new object by copying the prototypes.
Prototype mode is a
Create pattern。
structure
diagram-prototype pattern structure diagram
Prototype: Declares an interface to the clone itself.
Concreteprototype: Implements the specific operation of the clone itself.
Client: Call Prototype to clone itself, creating a new object.
Application Scenarios
When the class to instantiate is specified at run time, for example, by dynamic loading.
To avoid creating a factory class hierarchy that is parallel to the product class hierarchy.
When an instance of a class can have only one of several different state combinations. Building the corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time you use the appropriate state.
shallow copy and deep copy
Shallow copy means that the object referenced by the field is not copied when the field value of the object is copied.
For example, if an object has a field that points to a string, and we make a shallow copy of the object, then two objects will reference the same string.
A deep copy is when a class has resources, and when the object of that class has a replication process, the resource is redistributed, and the process is a deep copy.
Template method
The prototype pattern is mainly used for object replication, and its core is the prototype class prototype in the class diagram. The prototype class needs to have the following two conditions:
- Implements the Cloneable interface. In the Java language, there is a cloneable interface, which only works by notifying the virtual machine at runtime that it can safely use the Clone method on classes that implement this interface. In a Java virtual machine, only classes that implement this interface can be copied, or clonenotsupportedexception exceptions will be thrown at run time.
- Override the Clone method in the object class. In Java, the parent class of all classes is the object class, and the object class has a Clone method that returns a copy of the object, but its scope is protected type, and the generic class cannot be called, so The prototype class needs to modify the scope of the Clone method to the public type.
Prototype mode is a relatively simple mode, it is very easy to understand, implement an interface, rewrite a method to complete the prototype mode. In practical applications, prototype patterns rarely appear alone. Often mixed with other patterns, his prototype class prototype also uses abstract classes instead.
Code
classPrototypeImplementscloneable {
PublicPrototype Clone () {
Prototype Prototype =NULL;
Try{
Prototype = (prototype)Super. Clone ();
}Catch(Clonenotsupportedexception e) {
E.printstacktrace ();
}
returnPrototype
}
}
classConcreteprototypeextendsPrototype {
PublicvoidShow () {
SYSTEM.OUT.PRINTLN ("Prototype Pattern implementation class");
}
}
PublicclassPrototypepattern {
PublicStaticvoidMain (string[] args) {
Concreteprototype CP =NewConcreteprototype ();
for(inti=0; i< 10; i++) {
Concreteprototype CLONECP = (concreteprototype) cp.clone ();
Clonecp.show ();
}
}
}
Reference
"Big Talk design mode"
http://blog.csdn.net/zhengzhb/article/details/7393528
JAVA Design Pattern Prototype pattern