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 ?
-
1Don't implement the ICloneable interface: blogs.msdn.com/b/brada/archive/2004/05/03/125427.aspx– Mark SeemannMar 30, 2011 at 6:48
2 Answers
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.
-
That's actually not true. Design patterns are 'crutches' for missing functionality in the language. So with a good programming language you would inherit some or most of the patterns. See en.wikipedia.org/wiki/Software_design_pattern#Criticism– hyankovMar 14, 2016 at 21:18