You are on page 1of 3

PROTOTYPE

What is it?
The prototype pattern is from GoF Family. It allows multiple inheritance on
platforms that don't natively support such things.

What problem does it solve?

avoid subclasses of an object creator in the client application, like the abstract factory
pattern does.

avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new'
keyword) when it is prohibitively expensive for a given application.

How does it solve the problem??


It creates an independent clone object by copying all of the properties of an
existing object. This practice is particularly useful when the construction of a
new object is inefficient.

What are the main components i.e., main


elements?
Main elements are:
a. An instantiated object to be cloned
b. Clone method to clone the object
c. An interface clone (C#) or clonable (java)
with the clone
How do they relate to each other?

Whatre the strengths and drawbacks?


Strengths
Creates deep copy of the complex object hierarchy:
In order to create a deep copy of the complex object composition, this pattern helps to ease out
the work provided every object in the composition should implement ICloneable interface.
This is a very useful design pattern to copy objects such as trees.

1. Reduced load of initialization:


Every new object created using the clone method reflects the exact object state of the
original object. If we create many objects of the same class or structure by calling the class
constructor and then initialize every property explicitly for each object, this will increase the
number of repetitive lines needed to properly initialize every property of every object created.
This need of initialization can be reduced drastically by using this pattern. We can always
create a clone of the existing object created in the application to have the objects readily
initialized to the non-default/default state (Constructors are sufficient to initialize the object to
default state).

2. Reusability - Optimized coding efforts:


One object created in the system and initialized to the either default or non-default state is
sufficient to create the similar object copies again and again.
If there are only few properties that differ from object to object, we need not write the code
again and again to initialize the rest of the properties. This will optimize the coding efforts of
writing the code to initialize the properties that are different between objects of the same
class or structure.

3. Simplified object copy process:


Since the object copying is done recursively by calling the Clone method on every member
composition object, this makes the program structure easier to understand and maintain.

Drawbacks
Drawback to using the Prototype is that making a copy of an object can sometimes be complicated.

Example applications?
There are many practical scenarios where this design pattern really helps. Here follows few of them.

Session replication from one server to another server:


In an enterprise level application managing a server pool, the application will monitor and
maintain the optimum load on individual server in the pool. In case of the catastrophic failure,
one server may invalidate thousands of client connections. The enterprise application
managing the server pool can take the responsibility of cloning the sessions from one server
to another without disturbing the clients.

Generating the GUI having many numbers of similar controls:


This is a quiet frequent scenario. One can have a form or GUI that hosts many similar
controls. In order to maintain the consistency, one needs to initialize every object to the same
standard state. This process of initialization gets repeated again and again for each control
increasing the number of lines of code. In order to optimize this part of the code, one can
one have an object of a control initialized to a standard state and then replicate the same
object to create as many controls needed.

Include your sources (e.g., websites)?


http://www.csharpcorner.com/UploadFile/PashuSX/Pro
totypeDesignPattern05042006112741AM
/PrototypeDesignPattern.aspx

You might also like