0% found this document useful (0 votes)
4 views

Prototype Design pattern

Knowledge of proto type design pattern

Uploaded by

Vinoth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Prototype Design pattern

Knowledge of proto type design pattern

Uploaded by

Vinoth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Prototype Design pattern

-Vinoth S P
Java clone

• A clone is an exact copy of the original.


• The ability to create an object with similar state as the original object.
• So cloning is about creating the copy of original object.
• JVM will do cloning by,
For class only having primitive(int,float,short,byte,char…) then copy of
object will be created and the reference to new object will be returned.
class contains members of any class type then only the object references to
those members are copied and hence the member references in both the
original object as well as the cloned object refer to the same object.
Java Cloneable interface and clone() method

• In java, if a class needs to support cloning it has to do following


things:
• You must implement Cloneable interface.
• You must override clone() method from Object class.
• Cloned object will have separate memory address assignment.
• original and cloned objects should have same class type, but it is not
mandatory.
• original and cloned objects should have be equal using equals()
method, but it is not mandatory.
Cloning of Object
Department dept = new Department(1, "Human Resource");
Employee original = new Employee(1, "Admin", dept);
//Lets create a clone of original object
Employee cloned = (Employee) original.clone();
public class TestCloning {
public static void main(String[] args) throws CloneNotSupportedException {
Department hr = new Department(1, "Human Resource");
Employee original = new Employee(1, "Admin", hr);
Employee cloned = (Employee) original.clone();
//Let change the department name in cloned object and we will verify in original object
cloned.getDepartment().setName("Finance");
System.out.println(original.getDepartment().getName());
System.out.println(cloned.getDepartment().getName());
}
}
Output:
Finance
Finance
• Oops, cloned object changes are visible in original also. This way cloned objects can make havoc in the system if allowed to do so. Anybody can come and clone your
application objects and do whatever he likes. Can we prevent this??
• Answer is yes, we can. We can prevent this by creating Java deep copy and use copy constructors.
Java Deep Copy

• Deep clone is the desired behavior in most the cases. In the deep
copy, we create a clone which is independent of original object and
making changes in the cloned object should not affect original
object.

You might also like