Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Design & Architecture » Prototype Design Pattern: Easy and Powerful way to copy objects

Prototype Design Pattern: Easy and Powerful way to copy objects

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.

Page Views : 25541
Downloads : 373
Rating :
 Rate it
Level : Advanced
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
PrototypePatternCode.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Class Diagram:


 
Fig 1: Prototype Design Pattern.

Description:

  1. ICloneable Interface:
    This interface declares one method "Clone" that returns an object reference.

  2. 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.

  3. CloneableClass:
    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.

  4. ConsumerClass:
    This class consumes the CloneableClass object. It may create many similar copies of the CloneableClass based on the need.

Usage:

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.

Advantages:

  1. 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.

  2. 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).

  3. 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. 

  4. 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.

Application:

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.

Summary:

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Prashant Patil
Hi, this is Prashant here. I am working as a Systems Executive with the KPIT Cummins Info Systems Ltd. Pune. Talking about my experience I have total of 3.5 Yrs of experience on the various languages scripting as well as non-scripting. I had also worked on the VC++(( MFC and COM) for almost 2.5 yrs along with 8 months on Java(Core) . Currently I am working on the .Net/C# since 1.5 yrs. I had also worked on the scripting languages as such JavaScript, VB Script etc along with HTML. Apart form programming, I use to play a role of designer in my project and has through understanding of the design patterns and UML to it's best.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.