In general, a prototype is a template of any object before the actual object is constructed. This design pattern is useful when your application needs to create a number of similar instances of a class with small variations.
Also creation of a new object is a time taking process and an expensive too. So this pattern works by cloning of an object rather than creation.
When to use this pattern?
It allows making new instances by copying the existing instances. The copied cloned object is different from the original object. The state of the original is the same as the cloned one, at the time of cloning. Thereafter each object may undergo state change based on the operations performed on it. We can modify the objects to perform different things as well.
Please ensure that you want to deep clone or shallow clone your prototype because both will have different behaviour on runtime. If deep copy is needed, you can use a good technique given here using in memory serialization.
Shallow Clone:
When copied object contains references of other objects, the contained objects will not be cloned. Shallow clone copies only top level structure of an object. It can be done by using clone() method.
Deep Clone:
The object is copied along with the objects it refers to. Deep clone copies all the levels of the object from top to the bottom recursively. It can be done by using serialization or by overriding writing clone method in such way that to take all copied of contained objects.
Contained Object: Suppose Subject is a class which used by other classes.
Also creation of a new object is a time taking process and an expensive too. So this pattern works by cloning of an object rather than creation.
When to use this pattern?
- If the cost of creating the object is expensive or complicated.
- When trying to keep the number of classes in an application to a minimum.
- When adding or removing objects at runtime
- When the client application needs to be unaware of the object creation, composition and representation.
- Objects are required which are similar to the existing objects.
It allows making new instances by copying the existing instances. The copied cloned object is different from the original object. The state of the original is the same as the cloned one, at the time of cloning. Thereafter each object may undergo state change based on the operations performed on it. We can modify the objects to perform different things as well.
Please ensure that you want to deep clone or shallow clone your prototype because both will have different behaviour on runtime. If deep copy is needed, you can use a good technique given here using in memory serialization.
Shallow Clone:
When copied object contains references of other objects, the contained objects will not be cloned. Shallow clone copies only top level structure of an object. It can be done by using clone() method.
Deep Clone:
The object is copied along with the objects it refers to. Deep clone copies all the levels of the object from top to the bottom recursively. It can be done by using serialization or by overriding writing clone method in such way that to take all copied of contained objects.
Contained Object: Suppose Subject is a class which used by other classes.
package com.edu; public class Subject { private String subjectName; public Subject(String subj) { //Constructor this.subjectName = subj; } public String getSubjectName() { return subjectName; } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } }Implementing Shallow Clonning:
package com.edu.shallow; import com.edu.Subject; public class Student implements Cloneable { private String studentName; private Subject subject; //Contained Object public Student(String studName, String subjName) { this.studentName = studName; this.subject = new Subject(subjName); } @Override public Object clone() { //Shallow Copy try { return super.clone(); } catch(CloneNotSupportedException cnsExp) { return null; } } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Subject getSubject() { return subject; } public void setSubject(Subject subjectName) { this.subject = subjectName; } @Override public String toString() { return "studentName = " + studentName + ", subject = " + subject.getSubjectName(); } }Test Shallow Clonning:
package com.edu.shallow; public class ShallowCloneTest { public static void main(String[] args) { Student stud = new Student("Mohan", "Discrete Structures"); System.out.println("Original Object: " + stud.toString()); //Clone Object Student clonedStud = (Student) stud.clone(); System.out.println("Cloned Object: " + clonedStud.toString()); //Modify the Original Object stud.setStudentName("Rajesh"); stud.getSubject().setSubjectName("Computer Organisation"); System.out.println("Original Object after it is updated: " + stud.toString()); System.out.println("Cloned Object after updating original object: " + clonedStud.toString()); } }Shallow Clonning Testing Output:
Original Object: studentName = Mohan, subject = Discrete Structures Cloned Object: studentName = Mohan, subject = Discrete Structures Original Object after it is updated: studentName = Rajesh, subject = Computer Organisation Cloned Object after updating original object: studentName = Mohan, subject = Computer OrganisationImplementing Deep Clonning by overriding clone() method:
package com.edu.deep; import com.edu.Subject; public class Student implements Cloneable { private String studentName; private Subject subject; //Contained Object public Student(String studName, String subjName) { this.studentName = studName; this.subject = new Subject(subjName); } @Override public Object clone() { Student student = new Student(studentName, subject.getSubjectName()); return student; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Subject getSubject() { return subject; } public void setSubject(Subject subjectName) { this.subject = subjectName; } @Override public String toString() { return "studentName = " + studentName + ", subject = " + subject.getSubjectName(); } }Test Deep Clonning:
package com.edu.deep; public class DeepCloneTest { public static void main(String[] args) { Student stud = new Student("Mohan", "Discrete Structures"); System.out.println("Original Object: " + stud.toString()); //Clone Object Student clonedStud = (Student) stud.clone(); System.out.println("Cloned Object: " + clonedStud.toString()); //Modify the Original Object stud.setStudentName("Rajesh"); stud.getSubject().setSubjectName("Computer Organisation"); System.out.println("Original Object after it is updated: " + stud.toString()); System.out.println("Cloned Object after updating original object: " + clonedStud.toString()); } }Deep Clonning Test Output:
Original Object: studentName = Mohan, subject = Discrete Structures Cloned Object: studentName = Mohan, subject = Discrete Structures Original Object after it is updated: studentName = Rajesh, subject = Computer Organisation Cloned Object after updating original object: studentName = Mohan, subject = Discrete Structures
No comments:
Post a Comment