Design Patterns can help the designers solve the complex design problems with ease. There are many design patterns for the varying designing issues from the perspective of the object creation, object composition and object behavior.
The Prototype design pattern is a creational design pattern that helps create the deeply copied objects using the existing objects created in the application. This reduces the need of writing the repetitive code of creating and then copying the object state to the new object. The prototype design patter indeed does some complex job of creating the deep copy of the object.
This article mainly focuses on the Prototype design pattern along with advantages and possible practical scenarios where this pattern seems to be the best choice.
Fig 1: Prototype Design Pattern.
- ICloneable Interface:
This interface declares one method "Clone" that returns an object reference.
- CompositionClassAB and CompositionClassA:
These are the normal classes and structures that are active entities in the problem domain forming a complex composition of the object.
This is a sort of data class composed form the complex composition objects from the problem domain. The consumer/client can clone this object into a new object using the Clone method implemented by class; inherited from ICloneable.
This class consumes the CloneableClass object. It may create many similar copies of the CloneableClass based on the need.
The consumer class ConsumerClass can call the CloneableClass's Clone method in order to create similar copies of the objects. This Clone method implemented by CloneableClass in turn calls the Clone method recursively on the member object in order to create the deep copy of the CloneableClass.
- 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.
- 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).
- 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.
- 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.
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.
The Prototype design pattern is a simple yet powerful design pattern. Using this pattern one can easily get around the complex code that one thinks of copying one object hierarchy to another. Even though using this design pattern does not gain in terms of the performance, however it really scores on the basis of the neat and easy approach it uses to create a deep copy of the object hierarchy.