Creational - Prototype Pattern

Design Thoughts
The Prototype pattern delegates the cloning process to the actual object being cloned. The pattern declares a common interface for all objects that support cloning, which allows you to clone objects without coupling your code to the class to which the object belongs. Typically, such an interface contains only a single clone method.
This method creates an object of the current class and then copies all the member variable values of the original object to the newly created class. You can even copy private member variables, because most programming languages allow objects to access the private member variables of their own class.
Objects that support cloning are prototypes; when your object has dozens of member variables and hundreds of types, cloning it can even replace the construction of subclasses.
Class Diagram
Code Implementation
public class PrototypeTest {
interface Prototype {
Prototype clone();
}
class ConcretePrototype1 implements Prototype {
private int value;
public ConcretePrototype1(int value) {
this.value = value;
}
public ConcretePrototype1(ConcretePrototype1 prototype) {
this.value = prototype.value;
}
@Override
public Prototype clone() {
return new ConcretePrototype1(this);
}
}
class ConcretePrototype2 implements Prototype {
private int value;
public ConcretePrototype1(int value) {
this.value = value;
}
public ConcretePrototype2(ConcretePrototype1 prototype) {
this.value = prototype.value;
}
@Override
public Prototype clone() {
return new ConcretePrototype2(this);
}
}
public static void main(String[] args) {
ConcretePrototype1 prototype = new ConcretePrototype1(6);
ConcretePrototype1 newPrototype = prototype.clone();
}
}Pros and Cons
Advantages:
- Objects can be cloned without being coupled to the concrete classes they belong to.
- Complex objects can be generated more conveniently.
- Different configurations of complex objects can be handled in ways other than inheritance.
Disadvantages:
- Cloning complex objects containing circular references can be very troublesome.
- The implementation of the cloning method may need to consider the issues of deep cloning and shallow cloning.
Applicable Scenarios
- If you need to copy some objects and want the code to be independent of the specific classes to which these objects belong, you can use the prototype pattern.
- If the subclasses differ only in the way their objects are initialized, you can use this pattern to reduce the number of subclasses.