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
- Design Patterns: Introduction
- Learn Design Pattern - Singleton Pattern
- Learn Design Pattern - Factory Method Pattern
- Learn Design Pattern - Abstract Factory Pattern
- 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
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
- public class Address
- {
- public string State
- {get;set;}
-
- public string City
- {get;set;}
- }
Step 2
Create AuthorForShallowCopy Class
- public class AuthorForShallowCopy:ICloneable
- {
- public string Name
- {get;set;}
- public string TwitterAccount
- {get;set;}
- public string Website
- {get;set;}
- public Address HomeAddress
- {get;set;}
- public object Clone()
- {
- return this.MemberwiseClone();
- }
- }
Step 3
Create Client Code as
- Console.WriteLine("Shallow Copy Sample\n");
- AuthorForShallowCopy o = new AuthorForShallowCopy()
- {
- Name="Sukesh Marla",
- TwitterAccount = "https://twitter.com/SukeshMarla",
- Website="http://www.sukesh-marla.com",
- HomeAddress=new Address()
- {
- City="Mumbai",
- State= "Maharastra"
- }
- };
- Console.WriteLine("Original Copy");
- Console.WriteLine(o);
- AuthorForShallowCopy clonedObject = (AuthorForShallowCopy)o.Clone();
- Console.WriteLine("\nCloned Copy");
- Console.WriteLine(clonedObject);
- Console.WriteLine("\nMake Changes to clone copy address");
- clonedObject.Name = "Mr.Changer";
- clonedObject.TwitterAccount = "https://twitter.com/MrChanger";
- clonedObject.Website = "https://MrChanger.com";
- clonedObject.HomeAddress.State = "Karnataka";
- clonedObject.HomeAddress.City = "Manglore";
- Console.WriteLine("\nCloned Copy");
- Console.WriteLine(clonedObject);
- Console.WriteLine("\nOriginal Copy");
- Console.WriteLine(o);
Output
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:
- public class Address:ICloneable
- {
- public string State
- {get;set;}
- public string City
- {get;set;}
- public object Clone()
- {
- return this.MemberwiseClone();
- }
- }
Step 2
Create the AuthorForDeepCopy Class as:
- public class AuthorForDeepCopy:ICloneable
- {
- public string Name
- {get;set;}
- public string TwitterAccount
- {get;set;}
- public string Website
- {get;set;}
- public Address HomeAddress
- {get;set;}
- public object Clone()
- {
- AuthorForDeepCopy objPriCopy=(AuthorForDeepCopy) this.MemberwiseClone();
- objPriCopy.HomeAddress = (Address)this.HomeAddress.Clone();
- return objPriCopy;
- }
- }
Step 3
Create the Client Code as:
- Console.WriteLine("Deep Copy Sample\n");
- AuthorForDeepCopy o = new AuthorForDeepCopy()
- {
- Name="Sukesh Marla",
- TwitterAccount = "https://twitter.com/SukeshMarla",
- Website="http://www.sukesh-marla.com",
- HomeAddress=new Address()
- {
- City="Mumbai",
- State= "Maharastra"
- }
- };
- Console.WriteLine("Original Copy");
- Console.WriteLine(o);
- AuthorForDeepCopy clonedObject = (AuthorForDeepCopy)o.Clone();
- Console.WriteLine("\nCloned Copy");
- Console.WriteLine(clonedObject);
- Console.WriteLine("\nMake Changes to clone copy address");
- clonedObject.Name = "Mr.Changer";
- clonedObject.TwitterAccount = "https://twitter.com/MrChanger";
- clonedObject.Website = "https://MrChanger.com";
- clonedObject.HomeAddress.State = "Karnataka";
- clonedObject.HomeAddress.City = "Manglore";
- Console.WriteLine("\nCloned Copy");
- Console.WriteLine(clonedObject);
- Console.WriteLine("\nOriginal Copy");
- Console.WriteLine(o);
Output
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.