神刀安全网

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,

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();   }   

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.

class ExtraInformation   {       internal string Cost { get; set; }      internal int Ratings { get; set; }   }   

Now let’s have concrete prototype FourWheelar as in the following,

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();       }   }   

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.

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();   }   

Output

Design Patterns Simplified - Part 6 (Prototype)

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();   }   

Output

Design Patterns Simplified - Part 6 (Prototype)

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.

原文  http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part-6-prototype/

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Design Patterns Simplified – Part 6 (Prototype)

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮