This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Prototype Design Pattern

The prototype design pattern is a design pattern that is used to instantiate a class by copying, or cloning, the properties of an existing object. The new object is an exact copy of the prototype but permits modification without altering the original.

Testing the Prototype

The above implementation of the prototype pattern can be tested by creating and cloning an object of each concrete class. In the sample code below, a Developer object is instantiated and cloned. The Name property of the new Developer object is changed before string representations of both objects are outputted to the console. The outputted text shows that the properties have been cloned and are now independent. This process is then repeated with a Typist object, which is cloned before the copy has its name and words per minute properties modified.

Developer dev = new Developer();
dev.Name = "Bob";
dev.Role = "Team Leader";
dev.PreferredLanguage = "C#";

Developer devCopy = (Developer)dev.Clone();
devCopy.Name = "Sue";



Bob - Team Leader - C#
Sue - Team Leader - C#


Typist typist = new Typist();
typist.Name = "Kay";
typist.Role = "Typist";
typist.WordsPerMinute = 120;

Typist typistCopy = (Typist)typist.Clone();
typistCopy.Name = "Tim";
typistCopy.WordsPerMinute = 115;



Kay - Typist - 120wpm
Tim - Typist - 115wpm

3 September 2008