克隆(通过Cloneable接口或Copy构造函数)对象的性能与在原型模式中创建新对象的性能 [英] Performance of Cloning (either by Cloneable Interface or Copy Constructor) a object vs Creating a new object in Prototype Pattern

查看:53
本文介绍了克隆(通过Cloneable接口或Copy构造函数)对象的性能与在原型模式中创建新对象的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,在学习设计模式的同时,我了解到原型模式在需要创建大量对象的情况下非常有用且高效.

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.

原型模式还通过使用原型模式中的Cloneable接口或Copy构造函数来最大程度地减少创建过多对象的开销.

Prototype Pattern also minimizes the expense of too many object creations by making use of Cloneable interface or Copy Constructor in Prototype Pattern.

但是,我想知道克隆或复制一个对象比创建一个新对象更有效.JVM级别的解释会很好.

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?

推荐答案

原型模式未用于提高性能(尽管根据Ioannis的链接,该模式已用作性能改进).使用它是为了使您可以从(可能正在更改的)原型中创建新对象.

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.

需要某种克隆"方法,因此您不必关心原型的状态.您只需调用 prototype.someMethodThatReturnsACopy(),对象就可以使用了.您可以使用 clone()或其他某种方式来创建该副本,甚至可以根据需要手动构建副本.

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.

这篇关于克隆(通过Cloneable接口或Copy构造函数)对象的性能与在原型模式中创建新对象的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆