Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

prototype pattern and clone()

 
Peter Warde
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic