#Prototype Design Pattern

5 messages · Page 1 of 1 (latest)

unkempt zodiac
#

"The Prototype design pattern is a creational design pattern that involves creating new objects by copying existing ones, rather than creating them from scratch. In other words, the pattern involves creating a prototype object and then making copies of that object as needed, rather than creating new objects each time.
The Prototype pattern can be useful in situations where creating a new object is costly or time-consuming, and where the new object can be created by copying an existing one with a few modifications. By using the Prototype pattern, you can avoid the overhead of creating new objects each time, which can result in improved performance and reduced resource usage."

My question is:
How is creating a new object 'from scratch' (im assuming by calling its parametrized constructor and sending some data as args) any different than having a 'clone()' method which will call the same constructor and send it same args in the context of performance?

quasi plumeBOT
#

This post has been reserved for your question.

Hey @unkempt zodiac! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

ebon smelt
#

when you call clone(), the constructor isn't called. Method clone() is native method of Object class, it copies all fields as they are into a new object (that was created by just allocating memory, without calling constructor)

slim pilot