HI WELCOME TO Sirees

Prototype Design Pattern - C#

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

  1. public interface Prototype
  2. {
  3. Prototype Clone();
  4. }
  5. public class ConcretePrototypeA : Prototype
  6. {
  7. public Prototype Clone()
  8. {
  9. // Shallow Copy: only top-level objects are duplicated
  10. return (Prototype)MemberwiseClone();
  11. // Deep Copy: all objects are duplicated
  12. //return (Prototype)this.Clone();
  13. }
  14. }
  15.  
  16. public class ConcretePrototypeB : Prototype
  17. {
  18. public Prototype Clone()
  19. {
  20. // Shallow Copy: only top-level objects are duplicated
  21. return (Prototype)MemberwiseClone();
  22. // Deep Copy: all objects are duplicated
  23. //return (Prototype)this.Clone();
  24. }
  25. }

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

  1. /// <summary>
  2. /// The 'Prototype' interface
  3. /// </summary>
  4. public interface IEmployee
  5. {
  6. IEmployee Clone();
  7. string GetDetails();
  8. }
  9.  
  10. /// <summary>
  11. /// A 'ConcretePrototype' class
  12. /// </summary>
  13. public class Developer : IEmployee
  14. {
  15. public int WordsPerMinute { get; set; }
  16. public string Name { get; set; }
  17. public string Role { get; set; }
  18. public string PreferredLanguage { get; set; }
  19.  
  20. public IEmployee Clone()
  21. {
  22. // Shallow Copy: only top-level objects are duplicated
  23. return (IEmployee)MemberwiseClone();
  24.  
  25. // Deep Copy: all objects are duplicated
  26. //return (IEmployee)this.Clone();
  27. }
  28.  
  29. public string GetDetails()
  30. {
  31. return string.Format("{0} - {1} - {2}", Name, Role, PreferredLanguage);
  32. }
  33. }
  34.  
  35. /// <summary>
  36. /// A 'ConcretePrototype' class
  37. /// </summary>
  38. public class Typist : IEmployee
  39. {
  40. public int WordsPerMinute { get; set; }
  41. public string Name { get; set; }
  42. public string Role { get; set; }
  43.  
  44. public IEmployee Clone()
  45. {
  46. // Shallow Copy: only top-level objects are duplicated
  47. return (IEmployee)MemberwiseClone();
  48.  
  49. // Deep Copy: all objects are duplicated
  50. //return (IEmployee)this.Clone();
  51. }
  52.  
  53. public string GetDetails()
  54. {
  55. return string.Format("{0} - {1} - {2}wpm", Name, Role, WordsPerMinute);
  56. }
  57. }
  58.  
  59. /// <summary>
  60. /// Prototype Pattern Demo
  61. /// </summary>
  62.  
  63. class Program
  64. {
  65. static void Main(string[] args)
  66. {
  67. Developer dev = new Developer();
  68. dev.Name = "Rahul";
  69. dev.Role = "Team Leader";
  70. dev.PreferredLanguage = "C#";
  71.  
  72. Developer devCopy = (Developer)dev.Clone();
  73. devCopy.Name = "Arif"; //Not mention Role and PreferredLanguage, it will copy above
  74.  
  75. Console.WriteLine(dev.GetDetails());
  76. Console.WriteLine(devCopy.GetDetails());
  77.  
  78. Typist typist = new Typist();
  79. typist.Name = "Monu";
  80. typist.Role = "Typist";
  81. typist.WordsPerMinute = 120;
  82.  
  83. Typist typistCopy = (Typist)typist.Clone();
  84. typistCopy.Name = "Sahil";
  85. typistCopy.WordsPerMinute = 115;//Not mention Role, it will copy above
  86.  
  87. Console.WriteLine(typist.GetDetails());
  88. Console.WriteLine(typistCopy.GetDetails());
  89.  
  90. Console.ReadKey();
  91.  
  92. }
  93. }

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.