Introduction to the Proto type design pattern

No.of Views1176
Bookmarked0 times
Downloads 
Votes0
By  Chinna Srihari   On  22 Oct 2010 09:10:43
Tag : Design Patterns , General
A fully initialized instance to be copied or cloned. The Prototype pattern specifies the kind of objects to create using a prototypical instance.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

What is a Proto type design pattern?

GOF -> A fully initialized instance to be copied or cloned. The Prototype pattern specifies the kind of objects to create using a prototypical instance.

So first create an object that allows copying himself to another object of same type, instead of creating new objects by using ‘New’ keyword.

A.    Prototype co-opts one instance of a class for use as a breeder of all future instances.
B.    Prototype is unique among the other creational patterns in that it doesn’t require a class – only an object.
C.    Prototypes are useful when object initialization is expensive
D.    Factory Method creation through inheritance. Prototype creation through delegation
E.    Prototype doesn’t require sub classing, but it does require an “initialize” operation. Factory Method requires sub classing, but doesn’t require Initialize.

Architecture design(UML) 

Image Loading

C# Support for the Proto type

A.    Shallow Copy ->Object.MemberwiseClone Method

Any object that derives from the System.Object has this method, which creates a shallow copy of the current Object. The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object.

1.    Value Type

When a field is a value type, a bit-by-bit copy of the field is performed.

2.    Reference Type

When a field is a reference type, the reference is copied but the referred object is the same. So, the original object and its clone refer to the same object.  For example, consider an object called SomePerson that references objects Person. A shallow copy of SomePerson creates new object SomePerson1 that also references objects Person.

Look at the following example

public class A
        {private string _a;public A(string a){ 
 this._a = a;
        }public A Clone(){return (A)this.MemberwiseClone();
        }
}

On the client

 

Image Loading

Now consider another example

public class SomeData{private int myVar;public int MyProperty
        {get { return myVar; }set { myVar = value; }
        }
    }public class A{private string _a;public SomeData MySomeData;public A(string a){this._a = a;
        }public A Clone(){return (A)this.MemberwiseClone();
        }
    }

Client side 

Image Loading

B.    Deep Copy -> Object.MemberwiseClone Method

As said above Deep copy is altogether different. It creates new objects. Following is the example code to implement deep copy

public class SomeData
    {private int myVar;public int MyProperty
        {get { return myVar; }set { myVar = value; }
        }
    
    }public class A
    {private string _a;public SomeData MySomeData;public A(string a)
        {this._a = a;
        }public A Clone()
        {return (A)this.MemberwiseClone();
        }public A DeepCopy()
        {
            A a = (A)this.MemberwiseClone();
            a.MySomeData = new SomeData();
            a.MySomeData.MyProperty = this.MySomeData.MyProperty;return a;
        }
    }

 Client side code 

Image Loading

Hopes help. thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Chinna Srihari
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-India
Joined date-16 Jun 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
    Published Date : 23/Oct/2010
    This is a quite simple design pattern, let me tell you one thing there are many design patterns that they are implicitly singletons. I rather recommend to understanding this one first before going through other patterns.
    Published Date : 21/Oct/2010
    As a developer we might have noticed that creation of some class is very difficult and number of time that we need to create. Such object creation every time is very expensive in terms of system resources. If we can cache that kind of object show how will surely boost the application performance and resource usage can be saved. This where object pool design pattern will help development community to cache the objects. Some time it is also called as Object cache or Resource cache design pattern.
    Published Date : 21/Oct/2010
    The Command pattern creates distance between the client that requests an operation and the object that can perform it.
    Published Date : 20/Sep/2010
    Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.
    Published Date : 22/Oct/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</