Prototype Design Pattern in C#

Prototype Design Pattern in C# with Examples

In this article, I am going to discuss the Prototype Design Pattern in C# with examples. Please read our previous article where we discussed the Fluent Interface Design Pattern in C# with examples. The Prototype Design Pattern falls under the category of Creational Design Pattern. 

What is the Prototype Design Pattern?

As per the GoF Definition, “Prototype Design Pattern specifies the kind of objects to create using a prototypical instance, and create 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 clone 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 (=) copy 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.

Understanding Prototype Design Pattern in C#

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:

Prototype Design Pattern example in C#

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.

Understanding Object Cloining in C#

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 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.

The complete example 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.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:

Prototype Design Pattern in C#

Points to Remember:
  1. The MemberwiseClone method is part of the System.Object class and it creates a shallow copy of the given object. 
  2. MemberwiseClone Method only copies the non-static fields of the object to the new object
  3. 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.

While working with object cloning, we need to understand two things i.e. shallow copy and deep copy. So, in the next article, I am going to discuss the Shallow Copy and Deep Copy in C# with examples.

In this article, I try to explain the Prototype Design Pattern in C# step by step with some simple examples. I hope you understood the need and use of Prototype Design Pattern in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *