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:
- Design Patterns Simplified: Part 1
- Design Patterns Simplified - Part 2 (Singleton)
- Design Patterns Simplified - Part 3 (Simple Factory)
- Design Patterns Simplified - Part 4 (Abstract Factory)
- Design Patterns Simplified - Part 5 (Factory Method)
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,
- internal abstract class Vehicle
- {
- internal string VehicleType
- {
- get;
- set;
- }
- internal string Brand
- {
- get;
- set;
- }
- internal string Model
- {
- get;
- set;
- }
- internal abstract void ShowDetails();
- internal abstract Vehicle ShallowCopy();
- internal abstract Vehicle DeepCopy();
- internal ExtraInformation MoreInfo = new ExtraInformation();
- }
Here class Vehicle works like prototype. We have added class ExtraInformation to depict deep copy and shallow copy concepts.
- class ExtraInformation
- {
- internal string Cost { get; set; }
- internal int Ratings { get; set; }
- }
- internal class FourWheelar: Vehicle, ICloneable
- {
- internal override Vehicle ShallowCopy()
- {
- return this.MemberwiseClone() as Vehicle;
- }
- internal override Vehicle DeepCopy()
- {
- Vehicle vehicle = this.MemberwiseClone() as Vehicle;
- vehicle.MoreInfo = new ExtraInformation();
- vehicle.MoreInfo.Cost = this.MoreInfo.Cost;
- vehicle.MoreInfo.Ratings = this.MoreInfo.Ratings;
- return vehicle;
- }
- internal override void ShowDetails()
- {
- 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));
- }
- public object Clone()
- {
- return DeepCopy();
- }
- }
So setup is done now, let’s use this in client.
- static void Main()
- {
- Console.Title = "Prototype pattern demo";
- FourWheelar car = new FourWheelar();
- car.VehicleType = "Car";
- car.Brand = "Maruti";
- car.Model = "Swift";
- car.MoreInfo.Cost = "6.5 Lakhs INR";
- car.MoreInfo.Ratings = 2;
- FourWheelar truck = car.ShallowCopy() as FourWheelar;
- truck.VehicleType = "Truck";
- truck.Brand = "Tata";
- truck.Model = "ACE";
- truck.MoreInfo.Cost = "4.5 Lakhs INR";
- truck.MoreInfo.Ratings = 3;
- Console.WriteLine("******************************Shallow Copy******************************");
- Console.WriteLine("Origional Object:");
- car.ShowDetails();
- Console.WriteLine();
- Console.WriteLine("Shallow Cloned Object:");
- truck.ShowDetails();
- }

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,
- static void Main()
- {
- Console.Title = "Prototype pattern demo";
- FourWheelar car = new FourWheelar();
- car.VehicleType = "Car";
- car.Brand = "Maruti";
- car.Model = "Swift";
- car.MoreInfo.Cost = "6.5 Lakhs INR";
- car.MoreInfo.Ratings = 2;
- FourWheelar truckNew = car.Clone() as FourWheelar;
- truckNew.VehicleType = "Truck";
- truckNew.Brand = "Tata";
- truckNew.Model = "ACE";
- truckNew.MoreInfo.Cost = "4.5 Lakhs INR";
- truckNew.MoreInfo.Ratings = 3;
- Console.WriteLine("******************************Deep Copy******************************");
- Console.WriteLine("Origional Object:");
- car.ShowDetails();
- Console.WriteLine();
- Console.WriteLine("Deep Cloned Object:");
- truckNew.ShowDetails();
- }

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.

Prakash TripathiPosted Aug 5, 2016, 7:05 AM
Thnx Cao.
Cao FangshengPosted Aug 5, 2016, 4:37 AM
Good article.
Prakash TripathiPosted Apr 9, 2016, 10:41 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:36 AM
Nice article..
Prakash TripathiPosted Dec 31, 2015, 3:36 AM
Thnx Sibeesh.
Sibeesh VenuPosted Dec 31, 2015, 3:25 AM
Nice Share
Prakash TripathiPosted Dec 31, 2015, 12:19 AM
Thnx Raja
Prakash TripathiPosted Dec 31, 2015, 12:19 AM
Thnx Sreenivasa
Prakash TripathiPosted Dec 31, 2015, 12:19 AM
Thnx Santhakumar
Raja TPosted Dec 30, 2015, 9:57 PM
Nice, thanks for sharing
sreenivasa kPosted Dec 30, 2015, 4:13 PM
very nice
Santhakumar MunuswamyPosted Dec 30, 2015, 3:05 PM
Thanks for nice share
Prakash TripathiPosted Dec 30, 2015, 12:49 PM
Thnx Rupesh.
Rupesh KahanePosted Dec 30, 2015, 12:38 PM
nice share..
Prakash TripathiPosted Dec 30, 2015, 11:39 AM
Thnx Maruthi for bringing different point of view. Basically = operator doesn't work either with Shallow or Deep copied object as the object state is no longer the same. However it may work if you create copy like FourWheelar truckNew = car; but in this case any change made in the target object will also reflect in the source object including the Type, Brand and Model. You may test and let me know if you have different findings.
Prakash TripathiPosted Dec 30, 2015, 10:44 AM
Thnx Ankur
Prakash TripathiPosted Dec 30, 2015, 10:44 AM
Thnx Kiranteja
Prakash TripathiPosted Dec 30, 2015, 10:44 AM
Thnx Humayun.
Maruthi PalllamalliPosted Dec 30, 2015, 9:15 AM
Condition missing your logic ... Please check this ---- FourWheelar car = new FourWheelar(); and FourWheelar truckNew = car.Clone() as FourWheelar; ---------------------- if(car==trucknew) { // same refrence found } else { // Refrence mismatched }............... if copied object having same refrence means , Why should i use another refrence variable to display truck details ? Why can't show car details with trucknew refrence varaible.
Ankur MistryPosted Dec 30, 2015, 8:59 AM
Helpful stuff.
Kiranteja JallepalliPosted Dec 30, 2015, 7:32 AM
great work prakash
Humayun Kabir MamunPosted Dec 30, 2015, 7:13 AM
Nice...