Prototype pattern allow you to create objects by cloning existing ones.
Why do we need to clone objects?
1. You may want to pass an object to some other module for processing, and you want to ensure that the original instance is not accidently tampered with in the process
2. At times creating a new instance using the new keyword might add overhead to the object-creation process.
How clone objects?
The cloning operation is of two types — shallow copy and deep copy.
What is Shallow Copy?
In Shallow Copy simple type fields of a class are copied into the cloned instance. For reference type fields, only the reference pointer is copied (the values they hold are not copied).
What is Deep Copy?
In Deep Copy fields are copied into the cloned object. Thus, the original and the cloned object get independent copies of the reference type fields.
Leave a Reply