Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype Design Pattern vs ICloneable

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 ?

like image 256
kevin Avatar asked Mar 30 '11 02:03

kevin


People also ask

What is the difference between factory and prototype design pattern?

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.

What is advantage of prototype design pattern?

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.

When should we use prototype design pattern?

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.

What is the meaning of prototype design pattern?

(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.


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 :)

like image 127
Marcote Avatar answered Nov 11 '22 11:11

Marcote


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.

like image 37
Brent Stewart Avatar answered Nov 11 '22 11:11

Brent Stewart