Recently, while learning about Design Patterns, I learnt that the Prototype Pattern is very useful and performance efficient in scenarios where huge number of Object creation is needed.
Prototype Pattern also minimizes the expense of too many object creations by making use of Cloneable interface or Copy Constructor in Prototype Pattern.
But, I would like to know how does cloning or copying an object more efficient than creating a new object. A JVM level explanation would be great.
Is this the only reason Prototype Pattern is made used?
The prototype pattern isn't used for performance (although according to Ioannis' link, it has been used as a performance hack). It's used so you can create new objects from a (possibly changing) prototype.
Some method of "cloning" is needed so you don't have to care about the state of prototype. You can just call prototype.someMethodThatReturnsACopy()
and the object is ready for use. You could use clone()
or some other way to create that copy, even manually constructing one if you really want.