Design patterns: Prototype

Today, is about a fairly simple design pattern, we can say that this article will be a rest from the previous article about abstract factory.


Description and implementation method

The prototype is a pattern whose purpose is to copy objects from the parent object.

The prototype should be used where, we have to create a lot of instances of the same or similar objects, and cloning is more efficient than creating instances of objects with the word new.

To implement this pattern, we create a Prototype class that implements the ICloneable interface, if the client wants to copy the object calls the Clone() method. In .NET, this is how cloning looks like using the ICloneable interface, in Java or C++ cloning will look different.

In C# we will use here shallow cloning, I used to write about cloning, but I wrote about it very briefly to create shallow cloning, we create a class that inherits from the ICloneable interface and implements the Clone() method.



The UML diagram looks like this:

Scheme of Prototype

You can see that we have the ImageOne and ImageTwo classes that inherit from Image and, of course, I have the ImageHandler class that copies the Image class.



Let’s get to the practice, let’s go back to our example of a large Tesco store from the lesson about builder design pattern, let’s assume that the client wished to put up the next two identical stores, how to do it in the code level? Very simply 🙂 You just need to create the ICloneable interface first:

In this interface we have the Clone() method, which returns the value of the Object type, as we clone the store class, we will have to cast it to the object type, but how to do it will be further about that.

Next, we need to implement the ICloneable interface to the Shop class and create a method from the ICloneable interface in the Shop class:

Finally, in the client’s class, in the “Main” method, we copy the store object twice in this way:

As you can see in the code, to create copies of the Tesco store we project the Shop class in brackets on the call the Clone () method, because the Clone() method returns the Object type.

That’s all, we have created the same stores. The prototype is really a very simple pattern. At the end of the lesson, I will drop the source files into the lesson.

The Prototype pattern is mainly used for optimization purposes because cloning of the object is faster than its creation.


Example of cell division

We will make an example of cell cloning, which is well illustrated by the picture below:

Example of Prototype

Now let’s get to the code. I made sure it was a very simple example, so that you could understand the pattern even better. The whole code is below:

As you can see the example is really very simple, but in this example I did not use the ICloneable interface with .NET, I only did the cloning method myself, we clone the original object in the client and when cloning it we count the number of cells in the constructor, calling the Count() method.



Relations with other patterns



That’s all about Prototype 🙂.

In the next article, we will talk about the Object pool pattern.

This content also you can find on my steemit blog

And on medium:

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Notify about