Please enable Javascript to correctly display the contents on Dot Net Tricks!

Prototype Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 06 Jun 2013
Total Views : 40,279   
Updated On : 26 Sep 2016
 

Prototype pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is used to used to create a duplicate object or clone of the current object. It provides an interface for creating parts of a product. In this article, I would like share what is Prototype pattern and how is it work?

What is Prototype Pattern?

Prototype pattern is used to create a duplicate object or clone of the current object to enhance performance. This pattern is used when creation of object is costly or complex.

For Example: An object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.

Prototype Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Prototype design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. Prototype

    This is an interface which is used for the types of object that can be cloned itself.

  2. ConcretePrototype

    This is a class which implements the Prototype interface for cloning itself.

C# - Implementation Code

public interface Prototype
{
 Prototype Clone();
}
 
public class ConcretePrototypeA : Prototype
{
 public Prototype Clone()
 {
 // Shallow Copy: only top-level objects are duplicated
 return (Prototype)MemberwiseClone();
 
 // Deep Copy: all objects are duplicated
 //return (Prototype)this.Clone();
 }
}

public class ConcretePrototypeB : Prototype
{
 public Prototype Clone()
 {
 // Shallow Copy: only top-level objects are duplicated
 return (Prototype)MemberwiseClone();
 
 // Deep Copy: all objects are duplicated
 //return (Prototype)this.Clone();
 }
}

Prototype Pattern - Example

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

  1. IEmployee - Prototype interface

  2. Developer & Typist- Concrete Prototype

C# - Sample Code

/// <summary>
/// The 'Prototype' interface
/// </summary>
public interface IEmployee
{
 IEmployee Clone();
 string GetDetails();
}

/// <summary>
/// A 'ConcretePrototype' class
/// </summary>
public class Developer : IEmployee
{
 public int WordsPerMinute { get; set; }
 public string Name { get; set; }
 public string Role { get; set; }
 public string PreferredLanguage { get; set; }

 public IEmployee Clone()
 {
 // Shallow Copy: only top-level objects are duplicated
 return (IEmployee)MemberwiseClone();

 // Deep Copy: all objects are duplicated
 //return (IEmployee)this.Clone();
 }

 public string GetDetails()
 {
 return string.Format("{0} - {1} - {2}", Name, Role, PreferredLanguage);
 }
}

/// <summary>
/// A 'ConcretePrototype' class
/// </summary>
public class Typist : IEmployee
{
 public int WordsPerMinute { get; set; }
 public string Name { get; set; }
 public string Role { get; set; }

 public IEmployee Clone()
 {
 // Shallow Copy: only top-level objects are duplicated
 return (IEmployee)MemberwiseClone();

 // Deep Copy: all objects are duplicated
 //return (IEmployee)this.Clone();
 }

 public string GetDetails()
 {
 return string.Format("{0} - {1} - {2}wpm", Name, Role, WordsPerMinute);
 }
}

/// <summary>
/// Prototype Pattern Demo
/// </summary>

class Program
{
 static void Main(string[] args)
 {
 Developer dev = new Developer();
 dev.Name = "Rahul";
 dev.Role = "Team Leader";
 dev.PreferredLanguage = "C#";

 Developer devCopy = (Developer)dev.Clone();
 devCopy.Name = "Arif"; //Not mention Role and PreferredLanguage, it will copy above

 Console.WriteLine(dev.GetDetails());
 Console.WriteLine(devCopy.GetDetails());

 Typist typist = new Typist();
 typist.Name = "Monu";
 typist.Role = "Typist";
 typist.WordsPerMinute = 120;

 Typist typistCopy = (Typist)typist.Clone();
 typistCopy.Name = "Sahil";
 typistCopy.WordsPerMinute = 115;//Not mention Role, it will copy above

 Console.WriteLine(typist.GetDetails());
 Console.WriteLine(typistCopy.GetDetails());

 Console.ReadKey();

 }
}

Prototype Pattern Demo - Output

When to use it?

  1. The creation of each object is costly or complex.

  2. A limited number of state combinations exist in an object.

What do you think?

I hope you will enjoy the Prototype Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
11 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

Know More
6 MAR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
6 MAR
Angular 2 Master Class Fast Track Online Development Training (Online)

10:30 AM- 01:30 PM IST

Know More
27 FEB
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

Know More
25 FEB
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM-4:30 PM IST

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
14 FEB
.NET Development (Classroom)

11:00 AM-12:00 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (Classroom)

04:00 PM-05:30 PM

10 JAN
ASP.NET MVC with AngularJS Development (Online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

5 NOV
ASP.NET MVC with AngularJS Development (Classroom)

08:00 AM-09:30 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+