Learn Design Pattern - Prototype Pattern

In this article we will understand Prototype Pattern and try to implement it in an ASP.Net application.

So finally we are at the end of the Creational Pattern. Prototype is the last creational design pattern in the series.

Agenda

  • What is a Prototype Pattern?
  • Difference between Shadow and Deep Cloning.
  • Class Diagram.
  • Look into sample code and understand how to implement the Prototype Pattern.

Previous Articles

  1. Design Patterns: Introduction
  2. Learn Design Pattern - Singleton Pattern
  3. Learn Design Pattern - Factory Method Pattern
  4. Learn Design Pattern - Abstract Factory Pattern
  5. Learn Design Pattern - Builder Pattern

What is the Prototype Pattern?


The intent behind the usage of a Prototype pattern is for creation of an object clone; in other words it allows us to create a new instance by copying existing instances.

Difference between Shadow and Deep Cloning


The cloning falls under two categories: shallow and deep.

  • A shallow copy copies all reference types or value types, but it does not copy the objects that the references refer to. The references in the new object point to the same objects that the references in the original object points to.

    (Only Parent Object is cloned here).
     
  • In contrast, a deep copy of an object copies the elements and everything directly or indirectly referenced by the elements.

    (Parent Object is cloned along with the containing objects)

Class Diagram


class-diagram-Prototype-Pattern.jpg

Sample Code

In .Net, cloning can be achieved my making use of the ICloneable interface. Using the clone method, we can create clones or copies of instances with the same value as the existing instance.

Shallow Clone


Step 1

Create Address Class

  1. public class Address  
  2. {  
  3.     public string State  
  4.     {get;set;}  
  5.   
  6.     public string City  
  7.     {get;set;}   
  8. }  
Step 2

Create AuthorForShallowCopy Class

  1. public class AuthorForShallowCopy:ICloneable  
  2. {  
  3.           public string Name  
  4.           {get;set;}  
  5.           public string TwitterAccount  
  6.           {get;set;}  
  7.           public string Website  
  8.           {get;set;}  
  9.           public Address HomeAddress  
  10.           {get;set;}  
  11.           public object Clone()  
  12.           {  
  13.                     return this.MemberwiseClone();  
  14.           }  
  15. }  

 

Step 3

Create Client Code as

  1. Console.WriteLine("Shallow Copy Sample\n");  
  2. AuthorForShallowCopy o = new AuthorForShallowCopy()  
  3. {  
  4.           Name="Sukesh Marla",  
  5.           TwitterAccount = "https://twitter.com/SukeshMarla",  
  6.           Website="http://www.sukesh-marla.com",  
  7.           HomeAddress=new Address()  
  8.           {  
  9.                     City="Mumbai",  
  10.                     State= "Maharastra"  
  11.           }  
  12. };  
  13. Console.WriteLine("Original Copy");  
  14. Console.WriteLine(o);  
  15. AuthorForShallowCopy clonedObject = (AuthorForShallowCopy)o.Clone();  
  16. Console.WriteLine("\nCloned Copy");  
  17. Console.WriteLine(clonedObject);  
  18. Console.WriteLine("\nMake Changes to clone copy address");                             
  19. clonedObject.Name = "Mr.Changer";  
  20. clonedObject.TwitterAccount = "https://twitter.com/MrChanger";  
  21. clonedObject.Website = "https://MrChanger.com";  
  22. clonedObject.HomeAddress.State = "Karnataka";  
  23. clonedObject.HomeAddress.City = "Manglore";  
  24. Console.WriteLine("\nCloned Copy");  
  25. Console.WriteLine(clonedObject);  
  26. Console.WriteLine("\nOriginal Copy");  
  27. Console.WriteLine(o);  
Output

output-Shallow-Clone.jpg

We can see, cloning creates a new copy of the existing class but the Address remains the same for both classes.

(Any changes in the address of one class is reflected in both.)

Deep Clone


Step 1


Recreate the Address class as:

  1. public class Address:ICloneable  
  2. {  
  3.           public string State  
  4.           {get;set;}  
  5.           public string City  
  6.           {get;set;}  
  7.           public object Clone()  
  8.           {  
  9.                     return this.MemberwiseClone();  
  10.           }  
  11. }  
Step 2

Create the AuthorForDeepCopy Class as:

  1. public class AuthorForDeepCopy:ICloneable  
  2. {  
  3.           public string Name  
  4.           {get;set;}  
  5.           public string TwitterAccount  
  6.           {get;set;}  
  7.           public string Website  
  8.           {get;set;}  
  9.           public Address HomeAddress  
  10.           {get;set;}  
  11.           public object Clone()  
  12.           {  
  13.                     AuthorForDeepCopy objPriCopy=(AuthorForDeepCopy) this.MemberwiseClone();  
  14.                     objPriCopy.HomeAddress = (Address)this.HomeAddress.Clone();  
  15.                     return objPriCopy;  
  16.           }  
  17. }  
Step 3

Create the Client Code as:

  1. Console.WriteLine("Deep Copy Sample\n");  
  2. AuthorForDeepCopy o = new AuthorForDeepCopy()  
  3. {  
  4.           Name="Sukesh Marla",  
  5.           TwitterAccount = "https://twitter.com/SukeshMarla",  
  6.           Website="http://www.sukesh-marla.com",  
  7.           HomeAddress=new Address()  
  8.           {  
  9.                     City="Mumbai",  
  10.                     State= "Maharastra"  
  11.           }  
  12. };  
  13. Console.WriteLine("Original Copy");  
  14. Console.WriteLine(o);  
  15. AuthorForDeepCopy clonedObject = (AuthorForDeepCopy)o.Clone();  
  16. Console.WriteLine("\nCloned Copy");  
  17. Console.WriteLine(clonedObject);  
  18. Console.WriteLine("\nMake Changes to clone copy address");  
  19. clonedObject.Name = "Mr.Changer";  
  20. clonedObject.TwitterAccount = "https://twitter.com/MrChanger";  
  21. clonedObject.Website = "https://MrChanger.com";  
  22. clonedObject.HomeAddress.State = "Karnataka";  
  23. clonedObject.HomeAddress.City = "Manglore";  
  24. Console.WriteLine("\nCloned Copy");  
  25. Console.WriteLine(clonedObject);  
  26. Console.WriteLine("\nOriginal Copy");  
  27. Console.WriteLine(o);  
Output

output-Deep-Clone.jpg

The Parent is cloned with its contained objects.

So we are done with all 5 Creational Design Patterns.

Next we will start with Structural Design Patterns.

Hope you enjoyed reading the article, stay tuned for the next pattern.

Comments are always welcome.


Similar Articles
Just Compile LLP
Just Compile is an IT based firm based out of Mumbai. Application Development, Staffing, Training