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 }