Prototype
Definition
Example: Robot
The Problem: Student Example
Drawbacks
Solution: The Prototype Design Pattern
Class Diagram
Implementation
Resources
Video → 27. All Creational Design Patterns | Prototype, Singleton, Fac
tory, AbstractFactory, Builder Pattern
Definition
The Prototype design pattern is a creational pattern that allows you to create new objects by
cloning existing instances rather than creating them from scratch.
This is particularly useful when object creation is expensive or when you want to avoid the complexities involved
in creating objects using constructors.
Example: Robot
Creating each type of task-oriented robot from scratch is expensive. Instead, we clone the basic model of an
industrial robot (the prototype) and customize it for specific tasks.
The Problem: Student Example
1
2 // Concrete Class - Whose clone is to be created
3 public class Student {
4
5 int id;
6 String name;
7 String branch;
8 private int rollNo;
9
10 Student() {
11
12 }
13
14 Student(int id, String name, String branch, int rollNo) {
15 [Link] = id;
16 [Link] = name;
17 [Link] = branch;
18 [Link] = rollNo;
19 }
20
21 public void printDetails() {
22 [Link]("=== Student Details ===");
23 [Link](this + ": ");
24 [Link]("Id: " + id + ", Name: " + name + ",
Branch: " + branch + ", Roll No: " + rollNo);
25 }
26 }
27
28 // Client
29 public class DemoProblem {
30 public static void main(String[] args) {
31 Student studentOrg = new Student(1, "Aman", "CSE", 123);
32 [Link]();
33 // create a clone of the student object
34 Student studentClone = new Student();
35 [Link] = [Link];
36 [Link] = [Link];
37 [Link] = [Link];
38 // [Link] = [Link]; // Compilation
error
39 }
40 }
Drawbacks
Can’t access private fields outside the class during cloning. Ex: rollNo in the Student class.
It is not possible to be selective about the properties and behavior when cloning.
Solution: The Prototype Design Pattern
Instead of the client taking the responsibility of cloning the object, the cloning responsibility is assigned to the
actual concrete class implementing the prototype interface to clone itself.
The prototype interface ensures consistent implementation of cloning objects with features common to all
subtypes.
Class Diagram
Implementation
1 // Prototype interface
2 public interface StudentPrototype {
3 StudentPrototype clone();
4 }
5
6 // Concrete prototype class
7 public class Student implements StudentPrototype {
8
9 int id;
10 String name;
11 String branch;
12 boolean inHighSchool;
13 private int rollNo;
14
15 Student() {
16
17 }
18
19 // Clone constructor for efficient copying
20 Student(int id, String name, String branch, int rollNo) {
21 [Link] = id;
22 [Link] = name;
23 [Link] = branch;
24 [Link] = rollNo;
25 }
26
27 // setter method
28 public void setInHighSchool(boolean inHighSchool) {
29 [Link] = inHighSchool;
30 }
31
32 @Override
33 public StudentPrototype clone() {
34 return new Student(id, name, branch, rollNo);
35 }
36
37 public void printDetails() {
38 [Link]("=== Student Details ===");
39 [Link](this + ": ");
40 [Link]("Id: " + id + ", Name: " + name + ",
Branch: " + branch + ", Roll No: " + rollNo + ", In High School: " +
inHighSchool);
41 }
42 }
43
44 // Client
45 public class DemoSolution {
46 public static void main(String[] args) {
47 // Create initial prototypes (expensive operations)
48 Student student = new Student(5, "Rita", "CSE", 224);
49 [Link]();
50 // Clone objects (fast operations)
51 Student studentClone = (Student) [Link]();
52 [Link](true);
53 [Link]();
54 [Link]("Same object? " + (student ==
studentClone)); // false
55 }
56 }