Prototype Design Pattern in C#

This article explains how to implement prototype design pattern in C# and where it can be used.

Prototype design pattern comes under creational design pattern category of Gang of four (GoF) design patterns.

What Problem Prototype Pattern Solves?

Prototype pattern solves problem related with duplicating a class in C#. There are scenarios in our programs where we want to copy any object and proceed independently with the copied object without affecting the original object.

In C#, classes are reference type, so if you will copy the class object to another empty object and change the one object then it will also effect the other object, which is not desirable.

What is Prototype Design Pattern?

Prototype is used to clone/ duplicate the object. These cloned objects can be processed in the program independently and will possess no relation the original object.

Gang of Four (GoF) definition:
Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

Prototype Design Pattern – UML Diagram

Prototype design pattern in C# - UML Diagram

Participants in above UML diagram are:

IPrototype: Interface that is used for the Prototypes (objects) to clone itself

ConcretePrototype1, ConcretePrototype2: Implements IPrototype interface

Client: Client class will make clones of different objects

Shallow cloning and Deep cloning

The cloning of the object can be divided into two categories:

  • Shallow Cloning: In shallow cloning non static fields of the current object is copied in the new object. For the value type fields bit-by-bit copy of field is performed. For the reference type field, the reference/ address is copied but the referred object is not; so, both the original and cloned object will refer to the same instance of reference type fields. In C#, shallow cloning is performed using MemberWiseClone method.
  • Deep Cloning: Deep cloning basically duplicates everything. In deep cloning, both value type and reference type are copied in cloned object; original and cloned objects becomes two distinct object. So if cloned object instance is modified there will be no change in original object.

Prototype Design Pattern in C# – an Example

Let’s implement a simple example to understand prototype design pattern in C#.

Problem Statement

Let’s say we have a class called Employee class. Suppose you have to create multiple independent objects of the Employee class for any application. Since these objects are independent hence their fields may have different values (like name or age).

You can create these multiple independent objects of employee class by cloning already existing object using prototype pattern.Once the object will be cloned you can change their field.

Class Diagram

Protoype design pattern in C# - class diagram

Participants in above class diagram are:

IEmployee: IPrototype Interface

PermanentEmployee and TemporaryEmployee: ConcreteProtoype1 and ConcreteProtoype2 class

Program: Client class

Prototype Design Pattern in C# – Implementation Code

In C#, MemberWiseClone method is used to clone the objects. For deep cloning, MemberWiseClone method for every object should be called. First you should create one object then make clone of this object upon requirement.

Result

Prototype design pattern in C# - Output

Hope you get the idea about how to implement prototype design pattern in C#. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

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