Design Patterns (.NET) -Prototype Design Pattern Part-5
What is the Prototype Design Pattern?
Prototype Design Pattern specifies the kind of objects to create using a prototypical instance and creates new objects by copying this prototype.
To simplify the above definition, we can say that, the Prototype Design Pattern gives us a way to create new objects from the existing instance of the object. That means it clones the existing object with its data into a new object. If we do any changes to the cloned object (i.e. new object) then it does not affect the original object.
Note: The Prototype Design Pattern is unique among the other creational design patterns as it doesn’t require a class instead it requires an end object.
Understanding the Prototype Design Pattern in C#:
Let us understand the Prototype Design Pattern with an example. In C#, when we try to copy one object to another object using the assignment (=) operator, then both the objects will share the same memory address. And the reason is the assignment operator (=) copies the reference, not the object except when there is a value type field. This operator will always copy the reference, not the actual object. Please have a look at the following image.
As you can see in the above image, we have one Employee class. Then we create an instance of Employee class (i.e. emp1) and set its Name and Department properties. Then we create another employee instance (i.e. emp2) by assigning the existing employee object (i.e. emp1). When we change the Name of emp2 then it also changes the name of the emp1.?
The Complete Code is given below.
using System
namespace PrototypeDesignPattern
{
???class Program
???{
???????static void Main(string[] args)
???????{
???????????Employee emp1 = new Employee();
???????????emp1.Name = "Anurag";
???????????emp1.Department = "IT";
???????????Employee emp2 = emp1;
???????????emp2.Name = "Pranaya";
???????????Console.WriteLine("Emplpyee 1: ");
???????????Console.WriteLine("Name: " + emp1.Name + ", Department: " + emp1.Department);
???????????Console.WriteLine("Emplpyee 2: ");
???????????Console.WriteLine("Name: " + emp2.Name + ", Department: " + emp2.Department);
???????????Console.Read();
???????}
???}
???public class Employee
???{
???????public string Name { get; set; }
???????public string Department { get; set; }
???}
};
Output:
领英推è
Understanding Object Cloning:
When we talk about object cloning it means it is all about the call by value. So, if we do any changes to one object then it will not affect the other object. Let us see how to clone the object to another object. To do so, C# provides one method i.e. MemberwiseClone which will create a new complete copy of the object.
As shown in the above image, within the Employee class, we created one method i.e. GetClone and as part of that method, we are returning a clone object using the MemberwiseClone method. Then from the client code, first we are creating a new instance of the Employee class and assigning the properties with some values. Next, we are creating the second object by calling the GetClone method which in turn returns a new complete copy of the emp1 object. Now, both the objects (i.e. emp1 and emp2) are independent and if we do any changes to any object, then it will not affect other objects.
Prototype Design Pattern in C# with Example: Object Cloning
The following code example shows how to implement Prototype Design Pattern in C# using Object Cloning.
using System
namespace PrototypeDesignPattern
{
???class Program
???{
???????static void Main(string[] args)
???????{
???????????Employee emp1 = new Employee();
???????????emp1.Name = "Anurag";
???????????emp1.Department = "IT";
???????????Employee emp2 = emp1.GetClone();
???????????emp2.Name = "Pranaya";
??????????
???????????Console.WriteLine("Emplpyee 1: ");
???????????Console.WriteLine("Name: " + emp1.Name + ", Department: " + emp1.Department);
???????????Console.WriteLine("Emplpyee 2: ");
???????????Console.WriteLine("Name: " + emp2.Name + ", Department: " + emp2.Department);
???????????Console.Read();
???????}
???}
???public class Employee
???{
???????public string Name { get; set; }
???????public string Department { get; set; }
???????public Employee GetClone()
???????{
???????????return (Employee)this.MemberwiseClone();
???????}
???}
};
Output:
Points to Remember:
- The MemberwiseClone method is part of the System. Object class and creates a shallow copy of the given object.?
- MemberwiseClone Method only copies the non-static fields of the object to the new object
- In the process of copying, if a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referenced object is not.