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 ?
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through subclassing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned.
This approach saves costly resources and time, especially when object creation is a heavy process. The prototype pattern is a creational design pattern. Prototype patterns are required, when object creation is time consuming, and costly operation, so we create objects with the existing object itself.
Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. Prototype pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs.
(Learn how and when to remove this template message) The prototype pattern is a creational design pattern in software development. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With