table of Contents
Brief introduction
Prototype model (the Prototype)
specifies the kind of object prototype created instance, and create new objects by copying the prototype.
Prototype model is an
object creation schema
(
refer
design pattern creates schema
).
structure
FIG - prototype pattern configuration diagram
Prototype
: declare a cloning own interface.
ConcretePrototype : implement their own specific cloning operation.
Client : call Prototype to clone itself to create a new object.
ConcretePrototype : implement their own specific cloning operation.
Client : call Prototype to clone itself to create a new object.
motivation
When the class to be instantiated at run time are specified, e.g., by dynamic loading.
To avoid creating a parallel class hierarchy of factories and products when the class hierarchy.
When an instance of a class of state there is only one of several different combinations. Establishing a corresponding number of prototypes and clone them may be more convenient than the state of each hand with a suitable class is instantiated.
Shallow copy and deep copy
Shallow copy means that when the field value is copied object, the object reference fields are not copied.
For example: If an object has a pointer to the string field, and we made a shallow copy of the object, the two objects will reference the same string.
Deep copy means that when a class has the resources, when the objects of this class that occur during replication, and resource reallocation, this process is a deep copy.
For example: If an object has a pointer to the string field, and we made a shallow copy of the object, the two objects will reference the same string.
Deep copy means that when a class has the resources, when the objects of this class that occur during replication, and resource reallocation, this process is a deep copy.
Examples
Prototype model is mainly used for the duplicate object, which is the core prototypes Prototype class is a class diagram. Prototype class requires the following two conditions:
- Implement the Cloneable interface. In the java language has a Cloneable interface, its role is only one, is to tell the virtual machine at runtime can be safely achieved by using the clone class on this interface method. In the java virtual machine, only realized this interface can be copied, or at runtime will throw a CloneNotSupportedException.
- Method override clone Object class. Java, all classes are the parent class Object class, Object class has a clone method, is to return a copy of the object, but its scope is protected types, the general category can not be called, therefore, Prototype class needs to clone the method is modified to public scope type.
Code
class Prototype implements Cloneable {
public Prototype clone() {
Prototype prototype = null;
try {
prototype = (Prototype)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototype;
}
}
class ConcretePrototype extends Prototype {
public void show() {
System.out.println("原型模式实现类");
}
}
public class PrototypePattern {
public static void main(String[] args) {
ConcretePrototype cp = new ConcretePrototype();
for(int i=0; i< 10; i++){
ConcretePrototype clonecp = (ConcretePrototype)cp.clone();
clonecp.show();
}
}
}
public Prototype clone() {
Prototype prototype = null;
try {
prototype = (Prototype)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototype;
}
}
class ConcretePrototype extends Prototype {
public void show() {
System.out.println("原型模式实现类");
}
}
public class PrototypePattern {
public static void main(String[] args) {
ConcretePrototype cp = new ConcretePrototype();
for(int i=0; i< 10; i++){
ConcretePrototype clonecp = (ConcretePrototype)cp.clone();
clonecp.show();
}
}
}
Recommended Reading
This article is part of design patterns series .
Reference material
"Westward Design Patterns"
"HeadFirst Design Patterns"