Confusion in prototype pattern
Hi everybody!
I am having a difficulty in understanding the prototype pattern. Actually I got the idea behind it, but the example given in wikipedia for this pattern have confused me.
The code is as follows.
/** Prototype Class **/
publicclass Cookieimplements Cloneable{
public Object clone()
{
try{
//In an actual implementation of this pattern you would now attach references to
//the expensive to produce parts from the copies that are held inside the prototype.
return this.getClass().newInstance();// confused here.
}
catch(InstantiationException e)
{
e.printStackTrace();
returnnull;
}
}
}
/** Concrete Prototypes to clone **/
publicclass CoconutCookieextends Cookie{}
/** Client Class**/
publicclass CookieMachine
{
private Cookie cookie;//could have been a private Cloneable cookie;
public CookieMachine(Cookie cookie){
this.cookie = cookie;
}
public Cookie makeCookie(){
return (Cookie)cookie.clone();
}
public Object clone(){}
publicstaticvoid main(String args[]){
Cookie tempCookie =null;
Cookie prot =new CoconutCookie();
CookieMachine cm =new CookieMachine(prot);
for(int i=0; i<100; i++)
tempCookie = cm.makeCookie();
}
}
Now my confusion lies here.
>> return this.getClass().newInstance()
in the clone method.
On one hand it is said, we create clone of the object, but in reality new instance is created. I am not able to understand the logic.
Please help me out.
Thanks in Advance for any guidance.
I am really sorry -Kayaman-,
I didn't wanted to contradict you at all. But we are talking about a design pattern here, not about how to use clone() method. And there is no wrong or right way, but it's just about fulfilling the requirements in a particular scenario.
And in prototype model we need deep copy only, because we are not going to use the cloned object as it is, we would have to customize it according to the requirement later(which will customize the original references) which is not required in most of the cases.
It totally depends on the requirements about how we impliment the prototype pattern, but I think (it is totally a guess) that in most requirements deep copy will be required.
Please don't take it as contradiction, that was not at all the purpose.
> I am really sorry -Kayaman-,
No need to be.
> I didn't wanted to contradict you at all. But we are
> talking about a design pattern here, not about how to
> use clone() method. And there is no wrong or right
> way, but it's just about fulfilling the requirements
> in a particular scenario.
I'm talking about both implementing clone() and the design pattern.
There is a right way to implement clone() and that is calling super.clone() instead of a constructor. The additional use here is that it also copies all the references (shallowly). The comment there seems to indicate that "in a real implementation" the references would be copied shallowly by hand, which is something that clone() does automatically.
Then if you wanted to copy deeply some references, you could do that.
There is no contradiction, it's just that the clone() method is implemented incorrectly in the example.