DEV Community

Gary Woodfine
Gary Woodfine

Posted on • Originally published at garywoodfine.com on

The Prototype Design Pattern C# .net core

The Prototype Design Pattern is one of the Creational Design Patterns defined by the Gang Of Four (GOF) published their book Design Patterns: Elements of Reusable Object-Oriented Software in which they presented a catalog of simple and succinct solutions to commonly occurring design problems.

The Prototype pattern is specifically used when creating a duplicate object of an existing object while attempting to conserve resources and focus on performance.

Creational Design Patterns

Real World Implementation of the Prototype Pattern

Consider a real-world example of a Software Developer. In order to create or instantiate a new instance of Software Developer, we would in the first instance create a new Human Object, Nurture and Parent the Human object while educating it. Ensuring that the Human Object gets all the right attributes and elements which eventually lead to it becoming a Software Developer.

Creating a Software Developer object, is a very resource intensive and time consuming process. What happens you need another 10 of these objects ? Wouldn't it be great if we could just clone our existing copy of the software developer, which already has all the attributes and properties we need instantly?

This is the type pf scenario that Prototype Pattern serves to address. The pattern provides a prototype interface which enables creating 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.

Prototype Pattern Implementation

A typical implementation of the Prototype pattern could modelled as follows

Prototype pattern

GitHub logo garywoodfine / software-design-patterns

C# Design Patterns Tutorial Sample Code

Software Design Patterns in C# and .net core

In software development a Software Design Pattern is a reusable solution to commonly recurring problems. A software design pattern is a description or template used to solve a problem that can be used in many different situations.

In 1994, the so called Gang Of Four (GOF) published their book Design Patterns: Elements of Reusable Object-Oriented Software in which they presented a catalog of simple and succinct solutions to commonly occurring design problems.

The book captured 23 Patterns that enabled software architects to create flexible, elegant and ultimately reusable design patterns without having to rediscover or reinvent the design solutions for themselves.

Through a series of blog posts on garywoodfine.com I will discuss these patterns and more on how C# .net core developers can implement these patterns in cross platform .net core software solutions.

Contents

Software Design patterns are typically categorised into three…

A class may implement an ICloneable Interface which requires the class to implement a Clone method.

The .net framework provides developers with the ICloneable Interface

The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object. The ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond that supplied by Object.MemberwiseClone.

The ICloneable interface simply requires that your implementation of the Clone() method return a copy of the current object instance.

The developer is free to choose any implementation to perform the clone operation i.e. Deep, Shallow or Custom copy of the object.

Practical Implementation of Prototype Pattern

In the first simple implementation of the Prototype Pattern, we'll create a Developer class that implements ICloneable interface which preforms a MemberwiseClone operation.

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object.


public class Developer : ICloneable
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
         public List<string> Skills { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }

We can now implement our basic client App to instantiate an initial Developer object then we will create a Second version of the object making use of the Clone method that is available. We will then just inspect the cloned developer to check the FirstName it has been defined.

 class Program
    {
        static void Main(string[] args)
        {

            var dev = new Developer
            {
                FirstName = "Gary",
                Lastname = "Woodfine",
                 Skills = new List<string>{"C#", "PHP", "SQL", "JavaScript"}
            };


            var dev2 = dev.Clone() as Developer; 

            Console.WriteLine($"The Cloned  Developer name is { dev2.FirstName }  { dev2.Lastname }");

            Console.WriteLine("The second developer has the following skills: ");


            foreach (var skill in dev2.Skills)
            {
                Console.WriteLine(skill);
            }
        }
    }

If you run the application you'll notice that the Second instance of the Developer has all the same properties of the first one instantiated.

Applications for the Prototype Pattern

The Prototype Pattern is useful when you need to be able to quickly create new instances of objects based on other objects.

The following are typical application scenarios where you may want to consider using the Prototype pattern in C#

  • You want to instantiate classes at run time, for example, by dynamic loading.
  • Avoid building a class hierarchy of factories that parallels the class hierarchy of products.
  • When new instantiations of class can have one of only a few different combinations of state.
  • New object are going to be clones of existing object.
  • Avoid subclasses of an object creator in the client application i.e. Abstract Factory Pattern.
  • Avoid resource intensive object instantiation and initialisation logic.

Advantages of the Prototype Design Pattern

  • Reduce the time complexity to creating resource consuming objects by using the prototype pattern.
  • Reduces the sub-classing.
  • Enables adding and removing objects at run time.
  • Enables configuring application classes dynamically.

The Prototype Pattern is another tool you can use when you can specify the general class needed in program but need to defer the exact class until execution time. It is similar to the Builder Pattern in that some class or method decides what components or details make up the final instantiated class. However, it differs in that the target classes are constructed by cloning one or more classes and then changing or filling in the details of the cloned class to behave as desired.

Prototypes can be used whenever you need classes that differ only in the type of processing they offer.

Any change you make in one clone of the object is immediately reflected in the other because in fact there is only one object.

To see this behaviour lets add some additional logic to our code, to add an additional skill to our Second Developer instance. After that lets print out our skills of our initial instance to the console.

 class Program
    {
        static void Main(string[] args)
        {

            var dev = new Developer
            {
                FirstName = "Gary",
                Lastname = "Woodfine",
                Skills = new List<string>{"C#", "PHP", "SQL", "JavaScript"}
            };


            var dev2 = dev.Clone() as Developer; 

            Console.WriteLine($"The Cloned  Developer name is { dev2.FirstName }  { dev2.Lastname }");

            Console.WriteLine("The second developer has the following skills: ");


            foreach (var skill in dev2.Skills)
            {
                Console.WriteLine(skill);
            }

            // Add a new Skill to our Cloned Instance
            dev2.Skills.Add( "VueJs");

            Console.WriteLine(" ");


            Console.WriteLine("Our Initial Developer object now has VueJS added too");
            foreach (var skill in dev.Skills)
            {
                 Console.WriteLine(skill);
            }

        }

If we run our sample we'll see that the Vue.JS has been added to our Developer instance skill set.

The Cloned  Developer name is Gary  Woodfine
The second developer has the following skills: 
C#
PHP
SQL
JavaScript

Our Initial Developer object now has VueJS added too
C#
PHP
SQL
JavaScript
VueJs

Summary

The prototype pattern copies or clones an existing class, rather than creating a new instance, when creating new instances is more expensive.

Top comments (4)

Collapse
 
diagonallay profile image
DiagonAllay

It is feel like singleton but in another way

Collapse
 
gary_woodfine profile image
Gary Woodfine

It is no way like a singleton at all. Singleton by design is about limiting you class to only one instance and only one. Prototype enables making multiple copies of your class.

Collapse
 
diagonallay profile image
DiagonAllay

Sorry for misunderstand... I wonder what is the diffrent between multiple copies that changing in one instance of them will affect the other... While you just can use singleton instead

Thread Thread
 
gary_woodfine profile image
Gary Woodfine

prototype pattern lets you create new instances of a class by duplicating an already existing instance. You create new instance of class by copying an existing class.

Singleton pattern ensures there is only 1 of class. You do not create new instances of a singleton class, you only have 1 instance