It has been a while since I wrote about design patterns.
In the current post I'm going to explain the prototype pattern.
You can read my previous posts about design patterns here:
Abstract Factory pattern
What is Prototype Pattern?
The prototype is built upon the use of object cloning. The prototype creates new
objects by cloning one of its concrete classes. The prototype is used in the
- You need to hide the concrete product classes from the client.
- You want to reduce the number of classes to minimum.
- When you use dynamic loading.
- When you want to avoid using the complicated class hierarchy
- When your class has a small amount of different state combinations.
The prototype pattern can help to speed up instantiation of objects because
copying objects is faster than constructing objects.
For a UML diagram of the pattern go to dofactory site.
Example in C#
Lets look at an example of prototype usage:
#region The Prototype
public abstract class Prototype<T>
public T Clone()
// create a shallow copy of the object
#region Prototype Concrete
public class ConcretePrototype :
private string _strName;
/// The name of the prototype
public string Name
/// Construct a new ConcretePrototype with the
/// given name.
/// <param name="name">The given name</param>
public ConcretePrototype(string name)
_strName = name;
In the example I created a Prototype class which use the MemberwiseClone method
as the cloning implementation. You should remember that MemberwiseClone
makes a shallow copy of the object (copies object members). If you want a deep
copy you should implement it by yourself. The ConcretePrototype only adds
properties to the Prototype class.
Building Prototype Manager
You could build a manager to help you hold and handle the prototype concretes but
the manager isn't part of the pattern. An example of such a manager can look like:
#region Prototype Manager
/// The manager isn't part of the design pattern
/// you can use it to manage concretes as a helper
public class PrototypeManager
private Dictionary<string, ConcretePrototype> concretes =
new Dictionary<string, ConcretePrototype>();
/// Indexer for the concretes which are hold
/// in the PrototypeManager
public ConcretePrototype this[string name]
To sum up the post, the prototype pattern uses clone methods to make
application independent of how its products are created, composed
and represented. Prototype is easy to implement in C# because of the
A note, the prototype and the abstract factory patterns are competing patterns.
Knowing how to implement design patterns helps to understand the trade off
of using each pattern and to choose the right pattern to use.
The factory method will be the next pattern in the series.