private static void ShallowClone() { IMail mail = ProtoTypeFactory.Create <IMail>(); mail.From = "*****@*****.**"; mail.Content = "This is prototype pattern demo mail."; List <string> lstMainAddress = new List <string> { "*****@*****.**", "*****@*****.**" }; mail.Tos = lstMainAddress; ICloneable originallMail = mail as ICloneable; if (originallMail == null) { Console.WriteLine("Main cannot clone"); } IProvider provider = mail as IProvider; if (provider != null) { provider.SetValue("My Value"); } IMail newMail = originallMail.Clone() as IMail; lstMainAddress.Add("*****@*****.**"); Console.WriteLine(string.Format("From : {0}", newMail.From)); foreach (var strMailAddressTo in newMail.Tos) { Console.WriteLine(string.Format("To : {0}", strMailAddressTo)); } Console.WriteLine(string.Format("Content : {0}", newMail.Content)); provider = newMail as IProvider; if (provider != null) { Console.WriteLine(string.Format("Value : {0}", provider.GetValue())); } }
private static void DeepClone() { IMailEx mail = ProtoTypeFactory.Create <IMailEx>(); mail.From = "*****@*****.**"; mail.Content = "This is prototype pattern demo mail."; List <string> lstMainAddress = new List <string> { "*****@*****.**", "*****@*****.**" }; mail.Tos = lstMainAddress; ICloneable originallMail = mail as ICloneable; if (originallMail == null) { Console.WriteLine("Main cannot clone"); return; } IMailEx newMail = originallMail.Clone() as IMailEx; lstMainAddress.Add("*****@*****.**"); if (newMail != null) { Console.WriteLine(string.Format("From : {0}", newMail.From)); Console.WriteLine(string.Format("Content : {0}", newMail.Content)); foreach (var strMailAddress in newMail.Tos) { Console.WriteLine(string.Format("To : {0}", strMailAddress)); } } }
public static void Test() { ProtoTypeFactory protoTypeFactory = new ProtoTypeFactory(); var concreteProtoType1 = protoTypeFactory.Create <ConcreteProtoType1>(); var concreteProtoType2 = protoTypeFactory.Create <ConcreteProtoType2>(); }