In this post, We will talk and learn about Prototype Pattern in Java.
Prototype comes under a creational design pattern that makes use of cloning objects if object creation complex and time-consuming.
Below is the complete source code:
Student.java Model class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kkjavatutorials.prototype; /** * * @author KK JavaTutorials *Model class */ public class Student { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [Student id=" + id + ", Student name=" + name + "]"; } } |
StudentDAO.java creates Cloned Object that makes use of Prototype Pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.kkjavatutorials.prototype; import java.util.ArrayList; import java.util.List; /** * @author KK JavaTutorials * Dummy Student DAO implementation * In Real Scenario Object can be constructed by calling database. */ public class StudentDAO implements Cloneable{ private static List<Student> studentList; static { studentList = new ArrayList<>(); Student student1 = new Student(); student1.setId(10); student1.setName("PK"); Student student2 = new Student(); student2.setId(20); student2.setName("MK"); studentList.add(student1); studentList.add(student2); } /** * In Real Scenario Object can be constructed by calling database. * @return Student List */ public List<Student> getAllStudents(){ return studentList; } /** * Creating Clone of an Existing object */ @Override public List<Student> clone() throws CloneNotSupportedException { List<Student> dummyStudentList = new ArrayList<>(); for (Student student : studentList) { dummyStudentList.add(student); } return dummyStudentList; } } |
Test.java Client Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kkjavatutorials.prototype.client; import java.util.List; import com.kkjavatutorials.prototype.Student; import com.kkjavatutorials.prototype.StudentDAO; /** * @author KK JavaTutorials *Client Program which uses cloned object */ public class Test { public static void main(String[] args) throws CloneNotSupportedException { StudentDAO studentDAO = new StudentDAO(); //Getting Original copy of student list List<Student> allStudents = studentDAO.getAllStudents(); //Getting clone copy of student list List<Student> updatedStudentList = studentDAO.clone(); Student student = new Student(); student.setId(30); student.setName("SK"); //Doing manipulation on cloned copy updatedStudentList.add(student); System.out.println("Original Student List::"); allStudents.forEach(System.out::println); System.out.println("Updated Student List::"); updatedStudentList.forEach(System.out::println); } } |
Output of this program:
Original Student List::
Student [Student id=10, Student name=PK]
Student [Student id=20, Student name=MK]
Updated Student List::
Student [Student id=10, Student name=PK]
Student [Student id=20, Student name=MK]
Student [Student id=30, Student name=SK]
That’s all about Prototype Pattern in Java
You May Also Like:
Singleton Pattern
Factory Pattern
Abstract Factory Pattern
Builder Pattern
If you have any feedback or suggestion please feel free to drop in below comment box.
Really very helpful!