Design patterns: Prototype

Sławomir Kowalski
5 min readMay 1, 2018

--

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 ICloneableinterface, 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.

Structure

The UML diagram looks like this:

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.

Example

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:

public interface ICloneable
{
Object Clone();
}

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:

class Shop:ICloneable
{
//Other methods of the Shop class
public Object Clone()
{
return MemberwiseClone();
}
}

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

static void Main(string[] args)
{
IShopBuilder shopbuilder = new BigShopTesco();
ShopDirector shopdirector = new ShopDirector(shopbuilder);
shopdirector.buildShop();

Shop shop = shopdirector.getShop();
Console.WriteLine("Shop color: " + shop.GetWall().ReturnColorWall() + //green
"\nThickness of the shop wall: " + shop.GetWall().ReturnGageWall() +
"\nThe height of the roof: " + shop.GetRoof().ReturnHeightRoof() +
"\nNumber of tiles: " + shop.GetFloor().ReturnNumberOfTilesOnFloor() +
"\nThe length of the floor: " + shop.GetFloor().ReturnLenghtFloor() +
"\nThe type of tile size: " + shop.GetFloor().ReturnTypeGreatnessTiles());

Shop shop1 = (Shop)shop.Clone();
Console.WriteLine("The color of the second Tesco store: "+shop1.GetWall().ReturnColorWall());//green

Shop shop2 = (Shop)shop.Clone();
Console.WriteLine("The color of the third Tesco store: " + shop2.GetWall().ReturnColorWall());//green

Console.ReadKey();
}

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:

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:

namespace Cells
{
public abstract class Cell
{
public abstract Cell Split(int cellnumber);
}

class SingleCellOrganism : Cell
{
int cellnumber;

public SingleCellOrganism(int CellNumber)
{
cellnumber = CellNumber;
Count();
}

public override Cell Split(int cellnumber)
{
return new SingleCellOrganism(cellnumber);
}

public void Count()
{
Console.WriteLine("Cell number: "+ cellnumber);
}
}

class Program
{
static void Main(string[] args)
{
int cellnumber = 0;

SingleCellOrganism singlecell = new SingleCellOrganism(++cellnumber);

Cell cell1 = singlecell.Split(++cellnumber);
Cell cell2 = singlecell.Split(++cellnumber);
Cell cell3 = singlecell.Split(++cellnumber);
Cell cell4 = singlecell.Split(++cellnumber);

Console.ReadKey();
}
}
}

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.

Result:

Relations with other patterns

  • Often, different patterns use the factory method, for example, a builder or prototype when flexibility is needed.
  • The classes of the abstract factory are often implemented using the factory method, but can also be implemented using a prototype
  • Patterns The abstract factory, Builder and Prototype can be implemented as Singletons

Summary

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 https://steemit.com/design-patterns/@slawas/design-patterns-prototype

And on my blog devman: http://devman.pl/programtech/design-patterns-prototype/

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: Devman.pl-Sławomir Kowalski

– group on fb: DevmanCommunity

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

--

--