Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns For Dummies. The Prototype Pattern

Posted in Design Patterns
This post has been read 2861 times

Continuing with the series on Design patterns, I will take up Creational Patterns from today. I have written about Structural Patterns in my previous posts. You can get all the links for them here.

Creational Patterns

Creational patterns deals with separation of a system in terms of how their objects are created, what they are made of and how they must be represented. There are 5 types of creational patterns as observed by the GOF. I will write about the Prototype pattern today.

The Prototype

The prototype patterns deals with creation of new objects by cloning one of the available prototypes. The creation of objects is much faster for large classes. It also keeps a record of parts of a data structure so that it can be copied without knowing the subclass from which they were created.

    1 class PrototypePattern

    2 {

    3     [Serializable]

    4     public abstract class ProtoType<T> where T : class

    5     {

    6         public T ShallowCopy()

    7         {

    8             return this.MemberwiseClone() as T;

    9         }

   10 

   11         public T DeepCopy()

   12         {

   13             MemoryStream stream = new MemoryStream();

   14             BinaryFormatter formatter = new BinaryFormatter();

   15             formatter.Serialize( stream, this );

   16             stream.Seek( 0, SeekOrigin.Begin );

   17             T copy = formatter.Deserialize( stream ) as T;

   18             stream.Close();

   19             return copy;

   20         }

   21     }

   22 

   23     [Serializable]

   24     public class Address

   25     {

   26         public string Street { get; set; }

   27         public string City { get; set; }

   28         public string Country { get; set; }

   29 

   30         public Address( string street, string city, string country )

   31         {

   32             Street = street;

   33             City = city;

   34             Country = country;

   35         }

   36     }

   37 

   38     [Serializable]

   39     public class Person : ProtoType<Person>

   40     {

   41         public string Name { get; set; }

   42         public int Age { get; set; }

   43         public Address Address { get; set; }

   44 

   45         public Person( string name, int age, string street, string city, string country )

   46         {

   47             Name = name;

   48             Age = age;

   49             Address = new Address( street, city, country );

   50         }

   51 

   52         public override string ToString()

   53         {

   54             return string.Format( "Name : {0}\nAge : {1}\nAddress : {2}, {3}, {4}",

   55                 Name, Age, Address.Street, Address.City, Address.Country );

   56         }

   57     }

   58 

   59     class PersonManager : ProtoType<Person>

   60     {

   61         public Dictionary<string, Person> persons =

   62             new Dictionary<string, Person>()

   63                 {

   64                     { "person1", new Person("Person1", 25, "Street1", "City1", "Country1") },

   65                     { "person2", new Person("Person2", 26, "Street2", "City2", "Country2") }

   66                 };

   67     }

   68 

   69     static void Main()

   70     {

   71         PersonManager persManager = new PersonManager();

   72 

   73         Person person1, person2, person3, person4;

   74 

   75         person1 = persManager.persons["person1"];

   76         Console.WriteLine( person1.ToString() );

   77         // Output:  Name : Person1

   78         //          Age : 25

   79         //          Address : Street1, City1, Country1

   80 

   81         person3 = persManager.persons["person1"].ShallowCopy();

   82         person3.Name = "Person3";

   83         person3.Age = 20;

   84         person3.Address.Street = "Street3";

   85         person3.Address.City = "City3";

   86         person3.Address.Country = "Country3";

   87         Console.WriteLine( person3.ToString() );

   88         Console.WriteLine( person1.ToString() );

   89         // Output:  Name : Person3

   90         //          Age : 20

   91         //          Address : Street3, City3, Country3

   92         // Output:  Name : Person1

   93         //          Age : 25

   94         //          Address : Street3, City3, Country3

   95 

   96 

   97         person2 = persManager.persons["person2"];

   98         Console.WriteLine( person2.ToString() );

   99         // Output:  Name : Person2

  100         //          Age : 26

  101         //          Address : Street2, City2, Country2

  102 

  103 

  104         person4 = persManager.persons["person2"].DeepCopy();

  105         person4.Name = "Person4";

  106         person4.Age = 23;

  107         person4.Address.Street = "Street4";

  108         person4.Address.City = "City4";

  109         person4.Address.Country = "Country4";

  110         Console.WriteLine( person4.ToString() );

  111         Console.WriteLine( person2.ToString() );

  112         // Output:  Name : Person4

  113         //          Age : 23

  114         //          Address : Street4, City4, Country4

  115         // Output:  Name : Person2

  116         //          Age : 26

  117         //          Address : Street2, City2, Country2

  118 

  119         Console.ReadLine();

  120     }

  121 }

As you can see from the above example, the prototype pattern is implemented using cloning and serialization.  When we are using the shallow copied objects, the value of reference types present inside the class that was cloned will also be changed when we change the cloned object, where as in deep copied object this is not the case.  We can use the prototype pattern when we want to hide the concrete class from client or when we want to add or delete new classes dynamically.

In my next post I will talking about the Factory Method pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll