Design Patterns Simplified - Part 6 (Prototype)

I am here to continue the discussion around Design Patterns. Today we will discuss another creational design pattern called Prototype.

In case, you have not had a look at our previous articles, go through the following link:

Before talking about its implementation let’s begin with defining it.

Prototype is a design pattern which believes and enforces object cloning or copying over new object creation.

But object copy is that important

Answer is simple, copying objects improves performance in a situation when one needs lot of objects and creating them turns out to be a costly affair.

Since we are talking about copy let’s discuss shallow and deep copy as well.

Shallow Copy

As the name suggest, it’s a partial or incomplete copy and any reference types of the source object will not be copied and will keep on referring to the same memory location.

In .NET it is achieved by MemberwiseClone method.

Deep Copy

Similar to the name, it’s a real copy of all the members in the source object including reference types. For reference types, it creates a new memory location in heap to provide true copy experience.

We need to write custom code to achieve deep copy.

Now let’s talk about implementation of the pattern.

Assume we have an abstract class Vehicle that has the following structure,

  1. internal abstract class Vehicle  
  2. {  
  3.     internal string VehicleType  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     internal string Brand  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     internal string Model  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     internal abstract void ShowDetails();  
  19.     internal abstract Vehicle ShallowCopy();  
  20.     internal abstract Vehicle DeepCopy();  
  21.     internal ExtraInformation MoreInfo = new ExtraInformation();  
  22. }  
We can take interface as well but in that we need to override all the properties again which looks duplicate in this case.

Here class Vehicle works like prototype. We have added class ExtraInformation to depict deep copy and shallow copy concepts.
  1. class ExtraInformation  
  2. {   
  3.    internal string Cost { getset; }  
  4.    internal int Ratings { getset; }  
  5. }  
Now let’s have concrete prototype FourWheelar as in the following,
  1. internal class FourWheelar: Vehicle, ICloneable  
  2. {  
  3.     internal override Vehicle ShallowCopy()  
  4.     {  
  5.         return this.MemberwiseClone() as Vehicle;  
  6.     }  
  7.     internal override Vehicle DeepCopy()  
  8.     {  
  9.         Vehicle vehicle = this.MemberwiseClone() as Vehicle;  
  10.         vehicle.MoreInfo = new ExtraInformation();  
  11.         vehicle.MoreInfo.Cost = this.MoreInfo.Cost;  
  12.         vehicle.MoreInfo.Ratings = this.MoreInfo.Ratings;  
  13.         return vehicle;  
  14.     }  
  15.     internal override void ShowDetails()  
  16.     {  
  17.         Console.WriteLine(string.Format("Vehicle Type: {0} \tBrand: {1} \tModel: {2} \tCost: {3} \tRatings: {4}"this.VehicleType, this.Brand, this.Model, this.MoreInfo.Cost, this.MoreInfo.Ratings));  
  18.     }  
  19.     public object Clone()  
  20.     {  
  21.         return DeepCopy();  
  22.     }  
  23. }  
You can see here that we have also implemented ICloneable interface. To be frank, it’s not required here but I have added it to present its existence. It just have Clone method and when called, we are returning custom DeepCopy method.

So setup is done now, let’s use this in client.
  1. static void Main()  
  2. {  
  3.     Console.Title = "Prototype pattern demo";  
  4.     FourWheelar car = new FourWheelar();  
  5.     car.VehicleType = "Car";  
  6.     car.Brand = "Maruti";  
  7.     car.Model = "Swift";  
  8.     car.MoreInfo.Cost = "6.5 Lakhs INR";  
  9.     car.MoreInfo.Ratings = 2;  
  10.     FourWheelar truck = car.ShallowCopy() as FourWheelar;  
  11.     truck.VehicleType = "Truck";  
  12.     truck.Brand = "Tata";  
  13.     truck.Model = "ACE";  
  14.     truck.MoreInfo.Cost = "4.5 Lakhs INR";  
  15.     truck.MoreInfo.Ratings = 3;  
  16.     Console.WriteLine("******************************Shallow Copy******************************");  
  17.     Console.WriteLine("Origional Object:");  
  18.     car.ShowDetails();  
  19.     Console.WriteLine();  
  20.     Console.WriteLine("Shallow Cloned Object:");  
  21.     truck.ShowDetails();  
  22. }  
Output

run

You can see here that when we change the cost and ratings in the target object, original object is also getting changed. This is happening because the copied object is still holding the same reference as source.

Now let’s test deep copy,
  1. static void Main()  
  2. {  
  3.     Console.Title = "Prototype pattern demo";  
  4.     FourWheelar car = new FourWheelar();  
  5.     car.VehicleType = "Car";  
  6.     car.Brand = "Maruti";  
  7.     car.Model = "Swift";  
  8.     car.MoreInfo.Cost = "6.5 Lakhs INR";  
  9.     car.MoreInfo.Ratings = 2;  
  10.     FourWheelar truckNew = car.Clone() as FourWheelar;  
  11.     truckNew.VehicleType = "Truck";  
  12.     truckNew.Brand = "Tata";  
  13.     truckNew.Model = "ACE";  
  14.     truckNew.MoreInfo.Cost = "4.5 Lakhs INR";  
  15.     truckNew.MoreInfo.Ratings = 3;  
  16.     Console.WriteLine("******************************Deep Copy******************************");  
  17.     Console.WriteLine("Origional Object:");  
  18.     car.ShowDetails();  
  19.     Console.WriteLine();  
  20.     Console.WriteLine("Deep Cloned Object:");  
  21.     truckNew.ShowDetails();  
  22. }  
Output

Output

As you can see here that when we change the cost and ratings in the target object, original object is not getting changed. This is happening because the copied object is having new reference.

Hope you have liked the article. Looking forward for your comments/suggestions.

 

Up Next
    Ebook Download
    View all
    Learn
    View all