The pattern is useful when object creation is expensive or time-consuming, especially when an object contains many fields or requires complex initialization.
Instead of repeatedly constructing identical objects, an existing object is cloned and then modified as needed. This improves performance and simplifies object creation.
The Prototype Pattern is commonly used when creating configuration objects, templates, documents, game objects, and other objects that share a common initial state.
Structure
The Prototype Pattern consists of the following participants.1. The Prototype declares the method for cloning an object.
2. The Concrete Prototype implements the cloning logic.
3. The Client creates new objects by cloning an existing prototype instead of instantiating them directly.

Java Implementation
Suppose an application creates invoice templates. Creating a new invoice from scratch requires loading default settings, tax rules, and company information.Instead of repeating this initialization every time, an existing invoice template can be cloned. Create the prototype class.
public class Invoice implements Cloneable {
private String customer;
private String currency;
private String paymentTerms;
public Invoice(String customer,
String currency,
String paymentTerms) {
this.customer = customer;
this.currency = currency;
this.paymentTerms = paymentTerms;
}
public void setCustomer(String customer) {
this.customer = customer;
}
@Override
public Invoice clone() {
try {
return (Invoice) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return "Invoice{" +
"customer='" + customer + '\'' +
", currency='" + currency + '\'' +
", paymentTerms='" + paymentTerms + '\'' +
'}';
}
}
The client creates new invoices by cloning the existing template.
public class Main {
public static void main(String[] args) {
Invoice template =
new Invoice("Default Customer",
"USD",
"Net 30");
Invoice invoice1 = template.clone();
invoice1.setCustomer("Google");
Invoice invoice2 = template.clone();
invoice2.setCustomer("Microsoft");
System.out.println(invoice1);
System.out.println(invoice2);
}
}
Output:
Invoice{customer='Google', currency='USD', paymentTerms='Net 30'}
Invoice{customer='Microsoft', currency='USD', paymentTerms='Net 30'}
How It Works?
The client first creates an Invoice object that acts as the prototype. This object contains all the common configuration that should be shared by future invoices.Whenever a new invoice is required, the client calls the clone() method instead of creating a new object using the constructor. The cloned object initially contains the same field values as the prototype.
The client can then modify only the fields that differ, such as the customer name, while keeping the remaining configuration unchanged.
This approach avoids repeating expensive initialization logic and makes it easy to create multiple objects with a common initial state.
Advantages
1. It avoids expensive object creation by cloning existing objects.2. It simplifies the creation of complex objects with predefined configurations.
3. It reduces duplicate initialization code.
4. New objects can be customized after cloning without affecting the prototype.
Disadvantages
1. Implementing correct cloning can be difficult for complex object graphs.2. Objects containing nested mutable objects require deep copying instead of shallow copying.
3. The Cloneable interface and Object.clone() have several design limitations and are generally discouraged for complex applications.
JDK Examples
The Prototype Pattern appears throughout the Java platform.1. The Object.clone() method provides the fundamental cloning mechanism used by classes implementing the Cloneable interface.
2. Classes such as java.util.Date, java.util.Calendar, java.awt.Point, and many collection implementations support cloning to create copies of existing objects.
Summary
The Prototype Pattern creates new objects by cloning existing ones instead of constructing them from scratch.It is particularly useful when object creation is expensive or when multiple objects share the same initial configuration.