Introduction
Welcome to my Design Pattern For Beginners article series. In the previous two articles of the series, I explained the Singleton Design Pattern and Factory Design Pattern; you can read about them here:
- Design Pattern For Beginners - Part 1: Singleton Design Pattern
- Design Pattern For Beginners - Part 2: Factory Design Pattern
- Design Pattern For Beginners - Part 3: Prototype Design Pattern
- Design Pattern For Beginners - Part 4: Decorator Design Pattern
- Design Pattern For Beginners - Part 5: Composite Design Pattern
- Design Pattern For Beginners - Part 6: Adaptor Design Pattern
- Design Pattern For Beginners - Part 7: Bridge Design Pattern
- Design Pattern For Beginners - Part 8: memento Design Pattern
- Design Pattern for Beginners - Part-9: Strategy Design Pattern
- Design Pattern for Beginners - Part-10: Observer Design Pattern
- Design Pattern For Beginners - Part 11: Implement Decouple Classes in Application
In today's article, we will learn one more pervasive design pattern called the "Prototype Design Pattern."
As the name suggests, a prototype means copying something that exists. In this context, let me give one nice example. A few days ago, one of my friends went to a Tattoo shop to get a Tattoo on his body. (Yes, my friend is cool and rocking in nature.) After returning from the shop (with a tattoo in hand), he told me, Sourav, they have some preprepared styles that are much less costly, but if we want our design, they said: "it will take time, and the cost will be higher." I said Ok. Now consider this for the situation in software development. If the client demands an object we already have, we can deliver it quickly and at less cost. In software development, there are many scenarios where the exact copy of an existing object is frequently needed.
And this is the basic necessity of the Prototype Design Pattern. So, let's clarify the essential requirement "when we need to create the same object again and again, then we will implement a Prototype Design Pattern." Here is the simple logical diagram of the Prototype Design Pattern.

One big misconception occurs when we (basically junior programmers, including me) create the same object in C# or try to copy one object into another. Let's learn the problem first. Then we will see how to solve it.
Look at the following code; here, we will try to create a copy of an existing object.
using System;
using System.Collections;
using System.Globalization;
namespace Test1
{
class Test
{
public string Name;
}
class Program
{
static void Main(string[] args)
{
Test obj1 = new Test();
obj1.Name = "sourav";
Test obj2 = obj1; // Trying to make copy in obj2
obj2.Name = "c-sharpcorner.com";
Console.WriteLine(obj1.Name);
Console.ReadLine();
}
}
}
And here is the output.

In this code, we are trying to make a copy of one object into another by the following statement.
Test obj2 = obj1;




Dont Compar To OthersPosted Jan 2, 2020, 3:52 AM
Sir please explain Behavioral Pattrens
Rajan MishraPosted Oct 11, 2018, 6:19 AM
The final destination of my prototype pattern confusion thanks, coder.
Naga SankarPosted Sep 26, 2013, 12:20 PM
Superb article
Dinesh BeniwalPosted Aug 9, 2013, 3:41 AM
Belated Happy Bday Sourav
Sourav KayalPosted Aug 8, 2013, 11:21 PM
Thanks guys..
Anurag SarkarPosted Aug 8, 2013, 3:59 PM
Happy Birthday.
Sam HobbsPosted Aug 8, 2013, 3:44 PM
Happy birthday. Thank you for another interesting article, Sourav. I think however that prototype implies that something will be modified, not just copied.