C# | Prototype Design Pattern | Creational Design Pattern

Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. 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.

Code Syntax-

/// <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);

    }

}

----------------------------------------------------Program Class---------------------------------------------------    

internal 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 = "Arun"; //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();

    }

}


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.


Comments

Popular posts from this blog

C# | SOLID Principles

C# | Design Patterns