原型模式中的混乱 [英] Confusion in prototype pattern

查看:92
本文介绍了原型模式中的混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我很难理解原型模式。实际上我有了它背后的想法,但维基百科中给出的这个模式的例子让我很困惑。

代码如下。

展开 < span class =codeDivider> | 选择 | Wrap | Line数字

解决方案


现在我的困惑就在这里。

> >在克隆方法中返回this.getClass()。newInstance()



一方面据说,我们创建了对象的克隆,但实际上是新实例被建造。我无法理解逻辑。

(原型模式)维基百科上的链接)


在预先感谢任何指导。



''this''对象可以是Cookie或其任何派生对象(例如
CoconutCookie)。使用''getClass()''方法找到相应的类

,它可以创建该类的新对象。使用其中一种Cookie类型作为原型(或SmallTalk中的示例),它可以创建自己的新实例,其中
在功能上等同于clone()功能。如果wiki示例将方法命名为''duplicate()'而不是

''clone()'',那么你的混淆可能不存在
已经存在?


亲切的问候,


Jos



''this''对象可以是Cookie或其任何派生对象(例如,
CoconutCookie)。使用''getClass()''方法找到相应的类

,它可以创建该类的新对象。使用其中一种Cookie类型作为原型(或SmallTalk中的示例),它可以创建自己的新实例,其中
在功能上等同于clone()功能。如果wiki示例将方法命名为''duplicate()'而不是

''clone()'',那么你的混淆可能不存在
已经存在?


亲切的问候,


Jos



感谢您的回复。

但是我从原型模式的解释中得到的结果并未反映在代码中。


当对象创建和初始化相当时我们使用原型模式昂贵。我的意思是当我们想要获得现有复杂对象的精确副本(包括其当前状态)时。

他们所做的是创建一个新对象而不是复制它(现有的)在这种情况下,对象的状态不会被保留。


它不是让我感到困惑的方法的名称,但它们实际上已经覆盖了Object类的克隆方法。 />

请在这方面指导我。

再次感谢!



感谢您的回复。

但是我从原型模式的解释中得到的结果并没有反映在代码中。


我们使用原型模式时对象创建和初始化非常昂贵。我的意思是当我们想要获得现有复杂对象的精确副本(包括其当前状态)时。

他们所做的是创建一个新对象而不是复制它(现有的)在这种情况下,对象的状态不会被保留。


它不是让我感到困惑的方法的名称,但它们实际上已经覆盖了Object类的克隆方法。 />

请在此背景下指导我。

再次感谢!



好​​的!!!

我想我的答案就在这里

展开 | 选择 | Wrap | 行号

Hi everybody!
I am having a difficulty in understanding the prototype pattern. Actually I got the idea behind it, but the example given in wikipedia for this pattern have confused me.
The code is as follows.

Expand|Select|Wrap|Line Numbers

解决方案

Now my confusion lies here.
>> return this.getClass().newInstance()
in the clone method.
On one hand it is said, we create clone of the object, but in reality new instance is created. I am not able to understand the logic.

(Prototype Pattern link on wikipedia)

Thanks in Advance for any guidance.

The ''this'' object can be either a Cookie or any derived object thereof (such as a
CoconutCookie). The corresponding class is found using the ''getClass()'' method
which can create a new object of that class. Using one of the Cookie types as
a prototype (or ''exemplar'' in SmallTalk) it can create new instances of itself which
is functionally equivalent to the clone() functionality. Maybe your confusion wouldn''t
have existed if the wiki example had named the method ''duplicate()'' instead of
''clone()''?

kind regards,

Jos


The ''this'' object can be either a Cookie or any derived object thereof (such as a
CoconutCookie). The corresponding class is found using the ''getClass()'' method
which can create a new object of that class. Using one of the Cookie types as
a prototype (or ''exemplar'' in SmallTalk) it can create new instances of itself which
is functionally equivalent to the clone() functionality. Maybe your confusion wouldn''t
have existed if the wiki example had named the method ''duplicate()'' instead of
''clone()''?

kind regards,

Jos

Thanks jos for your reply.
But What I got from the explaination of prototype pattern is not getting reflected in the code.

We use prototype pattern when the object creation and initialization is quite expensive. I mean when we want to get the exact copy(including its current state) of the existing complex object.

And what they did is, created a new object instead of copying it(existing state of object is not retained in this case).

And its not the name of the method that have confused me, but they have actually overridden the clone method of Object class.

Please guide me in this context.
Thanks again!


Thanks jos for your reply.
But What I got from the explaination of prototype pattern is not getting reflected in the code.

We use prototype pattern when the object creation and initialization is quite expensive. I mean when we want to get the exact copy(including its current state) of the existing complex object.

And what they did is, created a new object instead of copying it(existing state of object is not retained in this case).

And its not the name of the method that have confused me, but they have actually overridden the clone method of Object class.

Please guide me in this context.
Thanks again!


Ok fine!!!
I think my answer lies here

Expand|Select|Wrap|Line Numbers


这篇关于原型模式中的混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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