aspose file tools*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes prototype pattern and clone() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "prototype pattern and clone()" Watch "prototype pattern and clone()" New topic
Author

prototype pattern and clone()

Peter Warde
Ranch Hand

Joined: Aug 18, 2004
Posts: 71
From my understand the prototype pattern in Java means clone(). What is exactly the difference between myClass.clone() and new MyClass()?
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
Not much difference. If you implement clone(), you should instantiate a new instance of the class and return it. In theory I suppose you could do just that (really, you CAN return any Object you want to), but what generally is done is some sort of carryover of the attributes of the class that is being cloned into a new instance of the class.

Useful IMHO for encapsulation.
Virendran Pushpanayagam
Ranch Hand

Joined: Jun 12, 2004
Posts: 31
Hi,

For example you have an Object that is very costly to create (new) in that case you use the clone function. The benefit is that you don�t have to go through all the trouble of creating a complex Object again.

Regards,
Viren.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: prototype pattern and clone()