Prototype Design Pattern
(Creational Design Pattern)
Mahbubur Rahman
Lecturer
Department of Software Engineering
Daffodil International University
What is Prototype Design Pattern?
Prototype Pattern is a Creational Design Pattern that allows you to
create new objects by copying (cloning) an existing object, instead
of creating new instances using constructors.
Object = Clone of an existing object
Solution with Prototype
● Create one base object (prototype)
● Clone it multiple times
● Modify only required fields
Why Prototype Pattern?
Problem:
• Creating objects with complex initialization can be costly.
• Many similar objects share configurations.
• We want to duplicate an existing instance, not rebuild it.
Web Browsers (Tabs & Sessions)
● When you open a new tab with same settings:
● Cache settings
● Extensions
● Settings
Browser clones the session prototype
Structure of Prototype Design Pattern
Steps of Prototype Design Pattern
● Create a base Prototype interface with clone()
method.
● Implement concrete classes that copy
themselves.
● Client asks the prototype to clone itself.
● Modify cloned object if necessary.
No new keyword for repetitive objects!
Example Scenario
Imagine an Employee Management System:
• Creating employee objects with multiple default attributes.
• Some employees share the same base data (department,
Role, salary).
● But differ in: Name, Employee ID
Prototype Pattern Solution Idea
● Create a Prototype Employee object (template).
● Clone it for new employees.
● Modify only unique fields.
Implementation of Prototype Design Pattern
Object Diagram:
Prototype Employee:
Name: Default, ID: 0, Department: IT,
Role: Software Engineer, Salary:
50000.0
Cloned Employees:
Name: Karim, ID: 101, Department: IT,
Role: Software Engineer, Salary:
50000.0
Name: Rahim, ID: 102, Department:
IT, Role: Software Engineer, Salary:
50000.0
Implementation of Prototype Design Pattern
Step-1: Create an Interface by declaring a clone() method.
public interface EmployeePrototype {
EmployeePrototype clone();
}
Explanation
● Declares a clone() method.
● Every employee must support cloning.
Implementation of Prototype Design Pattern
Step-2: Create Concrete Prototype by implementing the Interface.
public class Employee implements EmployeePrototype {
private String name, role, department;
private int id, salary;
public Employee(String name, int id, String department, String role, int salary) {
[Link] = name;
[Link] = id;
[Link] = department;
[Link] = role;
[Link] = salary;
}
// Custom clone method (manual copying)
public EmployeePrototype clone() {
return new Employee([Link], [Link], [Link], [Link], [Link]);
//Object Create
}
Step-2: Create Concrete Prototype by implementing the Interface.
// Setters for customization
public void setName(String name) {
[Link] = name;
}
public void setId(int id) {
[Link] = id;
}
public void showDetails() {
[Link]("Name: " + name +
", ID: " + id +
", Department: " + department +
", Role: " + role +
", Salary: " + salary);
}
}
Step-3: Create Main/Client class.
public class HRsystem {
public static void main(String[] args) {
// Step 1: Create Prototype Employee
EmployeePrototype prototypeEmp = new Employee(“Name", 000, "IT", "Software
Engineer", 50000); // prototypeEmp is the Base Object
// Step 2: Clone prototype for Employee 1
Employee emp1 = (Employee) [Link](); //clone object
[Link](“Karim");
[Link](111);
// Step 3: Clone prototype for Employee 2
Employee emp2 = (Employee) [Link](); //clone object
[Link](“Rahim");
[Link](121);
Step-3: Create Main/Client class.
[Link]("Prototype Employee:");
((Employee) prototypeEmp).showDetails();
[Link]("\nCloned Employees:");
[Link]();
[Link]();
}
}
Output:
Prototype Employee:
Name: Default, ID: 000, Department: IT, Role: Software Engineer, Salary: 50000.0
Cloned Employees:
Name: Karim, ID: 101, Department: IT, Role: Software Engineer, Salary: 50000.0
Name: Rahim, ID: 102, Department: IT, Role: Software Engineer, Salary: 50000.0
Thank You