原型设计模式与 ICloneable
我正在学习设计模式。今天我读到Prototype DP,发现它用于克隆对象。既然我们在.Net中有一个ICloneable接口,我们还需要Prototype DP吗?或者ICloneable
是否实现了Prototype DP?在两者之间进行选择有什么指导原则吗?
I am learning Design Patterns. Today I'm reading about Prototype DP and found that it's used for cloning an object. Since we have an ICloneable interface in .Net, do we still need the Prototype DP ? Or does ICloneable
implement the Prototype DP? Any guidelines for choosing between the two ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅实现像 ICloneable 这样的接口并不意味着您遵循某种模式,一切都取决于您想要实现的意图。如果你愿意的话,这有点哲学,但我只是想强调这一点。模式具有力量和意图,并且根据定义是常见问题的通用解决方案。
在这个特定的示例中,是的,正确实现接口可以引导您了解模式的意图,事实上,维基百科文章使用
ICloneable
接口作为用 Java 编写的示例:http://en.wikipedia.org/wiki/Prototype_pattern。当然,您可以使用另一种方法,使用您的另一个接口,这并不是使用
ICloneable
接口的模式的要求。希望这对您有所帮助,欢迎来到模式世界:)
By implementing just an interface like
ICloneable
doesn't mean you're following a pattern, all depends in the intent you're trying to achieve. This is somewhat philosophical if you will, but I just want to be sure to stress this. Patterns have forces and intents, and by definition is a general solution for a common problem.In this particular example, yes, implementing correctly the interface can lead you to the intent of the pattern, in fact the wikipedia article uses the
ICloneable
interface for its exameple written in Java : http://en.wikipedia.org/wiki/Prototype_pattern.Of course you can use another approach, using another interface of yours, is not a requirement for the pattern to use the
ICloneable
interface.Hope this helps and welcome to the world of patterns :)
设计模式不是语言固有的东西,而是一般问题的通用解决方案。它们是一个概念,可以通过多种不同的方式和多种不同的语言来实现。
Design patterns are not something that are inherent in a language, but they are a common solution to a general problem. They are a concept and can be implemented in many different ways and many different languages.