Understanding the Prototype Design Pattern with Java Examples
This article explains the Prototype creational design pattern, its definition, key concepts, typical usage scenarios, UML illustrations, and provides a complete Java implementation with code examples, output analysis, and a discussion of its application in Spring's prototype‑scoped beans.
The article introduces the Prototype design pattern, a creational pattern that creates new objects by copying an existing instance (the prototype) instead of constructing them with the new keyword.
According to Wikipedia, the pattern’s core idea is to return a new instance by cloning a customizable prototype. The key terms are prototype instance, copy, and new instance.
An illustrative example uses a Monkey class (the prototype) and a Monster class (non‑cloneable). By cloning the original monkey ("Monkey 1"), a second monkey ("Monkey 2") is created to fight a monster, demonstrating how the prototype object is duplicated.
The article raises two questions: whether every class can be cloned and whether the cloned object inherits all abilities of the original. It hints at deep vs. shallow copying in Java.
Typical usage scenarios are described: (1) when the class name is unknown, making new impossible, and (2) when a large number of complex objects must be created efficiently, where cloning can improve performance.
UML diagrams (shown as images) depict the roles and relationships among the prototype, concrete prototype, and client.
Code demonstration is provided in four steps:
Define MonkeyPrototype implementing Cloneable and overriding clone() . package com.silence.arts.pattern.creator.prototype; import lombok.Data; import java.util.ArrayList; /** * Function: Monkey prototype class */ @Data public class MonkeyPrototype implements Cloneable { private Integer age; private String name; private ArrayList things; private ExtraSkill extraSkill; private MonkeyPrototype monkeyPrototype = null; @Override public MonkeyPrototype clone() { try { monkeyPrototype = (MonkeyPrototype) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return monkeyPrototype; } }
Extend the prototype with Monkey and add a message() method. package com.silence.arts.pattern.creator.prototype; /** * Function: Monkey concrete class */ public class Monkey extends MonkeyPrototype { /** * Output content */ public void message() { System.out.println(this.getName() + "-" + this.getAge() + "-" + this.getThings() + "- 金箍棒长度:" + this.getExtraSkill().getWidth()); } }
Create ExtraSkill to hold additional attributes. package com.silence.arts.pattern.creator.prototype; /** * Function: Extra skill class */ public class ExtraSkill { private int width; private int height; public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }
Write a client MonkeyClient that creates a prototype monkey, clones it five times, modifies each clone, and prints the results. package com.silence.arts.pattern.creator.prototype; import java.util.ArrayList; /** * Function: Client to test cloning */ public class MonkeyClient { public static void main(String[] args) { // Create original monkey Monkey monkey = new Monkey(); monkey.setName("猴哥一号"); monkey.setAge(18); ArrayList list = new ArrayList<>(); list.add("七十二变化"); list.add("火眼金睛"); monkey.setThings(list); ExtraSkill m = new ExtraSkill(); m.setWidth(1920); m.setHeight(1080); monkey.setExtraSkill(m); // Clone five monkeys System.out.println("--------------------------克隆猴哥----------------------------"); for (int i = 0; i < 5; i++) { Monkey cloneMonkey = (Monkey) monkey.clone(); cloneMonkey.setAge(cloneMonkey.getAge() + i); cloneMonkey.setName(cloneMonkey.getName() + i); ArrayList innerThing = cloneMonkey.getThings(); innerThing.add("特殊技能 " + i); cloneMonkey.setThings(innerThing); cloneMonkey.getExtraSkill().setWidth(cloneMonkey.getExtraSkill().getWidth() + i * 100); cloneMonkey.getExtraSkill().setHeight(cloneMonkey.getExtraSkill().getHeight() + i * 100); System.out.println(cloneMonkey.getExtraSkill()); cloneMonkey.message(); System.out.println(); } System.out.println("--------------------------猴哥一号----------------------------"); System.out.println(monkey.getExtraSkill()); monkey.message(); } }
The program output shows that the five cloned monkeys have independent ages and names, but they share the same mutable things list, illustrating a shallow‑copy issue.
The article then discusses the application of the Prototype pattern in Spring Framework, where beans are singleton by default; using @Scope("prototype") makes Spring create a new bean instance for each request. It notes that Prototype is often combined with Factory pattern.
Finally, the author summarizes that the article covered the definition, a concrete Java example, usage scenarios, and Spring integration, encouraging readers to continue learning design patterns and join the community.
Promotional links and QR‑code images are included at the end of the original article.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.