Prototype Pattern Explained with Code and Real‑World Scenarios
This article introduces the Prototype design pattern, explains its purpose of cloning existing objects to avoid costly construction, illustrates practical scenarios such as product templates and game asset generation, provides a complete Java shallow‑copy example, and highlights common pitfalls and differences from the Factory pattern.
Prototype Pattern Overview
Use an existing fully‑initialized object as a template and clone it to create a new object, avoiding the cost of a complex construction process. The core purpose is to reduce repeated creation overhead and quickly generate similar objects.
Real‑World Scenarios
Copying documents: duplicate a manuscript without re‑writing.
Software: product templates where only a few parameters change to produce many variants.
Report templates, form drafts, batch generation of game monsters.
Java Implementation (Shallow Copy Example)
1. Goods class implementing Cloneable
public class Goods implements Cloneable{
private String name;
private double price;
public Goods(String name, double price) {
this.name = name;
this.price = price;
}
// Override clone method
@Override
public Goods clone() throws CloneNotSupportedException {
return (Goods) super.clone();
}
// setter for name
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "商品:"+name+",价格:"+price;
}
}2. Test cloning
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
// Create prototype template
Goods template = new Goods("T恤", 99.9);
// Clone new objects
Goods goods1 = template.clone();
goods1.setName("白色T恤");
Goods goods2 = template.clone();
goods2.setName("黑色T恤");
System.out.println(goods1);
System.out.println(goods2);
}
}Common Business Scenarios
High object creation cost (e.g., database queries, complex initialization) with frequent duplication.
Large numbers of similar objects differing only in a few fields (products, form templates).
Caching template data to quickly generate many instances.
Game development: batch creation of monsters, NPCs, items.
Draft saving, form copying, report template reuse.
Pitfalls
Java’s default clone performs a shallow copy; when a class contains reference‑type members, a deep copy must be implemented manually to avoid shared mutable state.
Difference from Factory Pattern
Factory pattern creates objects from scratch using new, while prototype pattern copies an existing object, skipping the initialization steps.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
