Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

java - Performance of Cloning (either by Cloneable Interface or Copy Constructor) a object vs Creating a new object in Prototype Pattern

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...