آشنایی با الگوی طراحی Prototype
نویسنده: وحید فرهمندیان
تاریخ: ۱۳۹۳/۱۲/۰۴ ۱۹:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public abstract class APrototype : ICloneable {
public string Name { get; set; }
public string Health { get; set; }
}
public class Prototype : APrototype
{
public override string ToString() { return string.Format("Player name: {0}, Health statuse: {1}", Name, Health); }
} Prototype p1 = new Prototype { Name = "Vahid", Health = "OK" };
Console.WriteLine(p1.ToString()); public abstract class APrototype : ICloneable
{
public string Name { get; set; }
public string Health { get; set; }
public abstract object Clone();
}
public class Prototype : APrototype
{
public override object Clone() { return this.MemberwiseClone() as APrototype; }
public override string ToString() { return string.Format("Player name: {0}, Health statuse: {1}", Name, Health); }
} Prototype p1 = new Prototype { Name = "Vahid", Health = "OK" };
Prototype p2 = p1.Clone() as Prototype;
Console.WriteLine(p1.ToString());
Console.WriteLine(p2.ToString()); public abstract class APrototype : ICloneable
{
public string Name { get; set; }
public string Health { get; set; }
public AdditionalDetails Detail { get; set; }
public abstract object Clone();
}
public class AdditionalDetails { public string Height { get; set; } }
public class Prototype : APrototype
{
public override object Clone() { return this.MemberwiseClone() as APrototype; }
public override string ToString() { return string.Format("Player name: {0}, Health statuse: {1}, Height: {2}", Name, Health, Detail.Height); }
} Prototype p1 = new Prototype { Name = "Vahid", Health = "OK", Detail = new AdditionalDetails { Height = "100" } };
Prototype p2 = p1.Clone() as Prototype;
p2.Detail.Height = "200";
Console.WriteLine(p1.ToString());
Console.WriteLine(p2.ToString()); public abstract class APrototype : ICloneable
{
public string Name { get; set; }
public string Health { get; set; }
//This is a ref type
public AdditionalDetails Detail { get; set; }
public abstract APrototype ShallowClone();
public abstract object Clone();
}
public class AdditionalDetails { public string Height { get; set; } }
public class Prototype : APrototype
{
public override object Clone()
{
Prototype cloned = MemberwiseClone() as Prototype;
//We need to deep copy each ref types in order to prevent shallow copy
cloned.Detail = new AdditionalDetails { Height = this.Detail.Height };
return cloned;
}
//Shallow copy will copy ref type's address instead of their value, so any changes in cloned object or source object will take effect on both objects
public override APrototype ShallowClone() { return this.MemberwiseClone() as APrototype; }
public override string ToString() { return string.Format("Player name: {0}, Health statuse: {1}, Height: {2}", Name, Health, Detail.Height); }
} Prototype p1 = new Prototype { Name = "Vahid", Health = "OK", Detail = new AdditionalDetails { Height = "100" } };
Prototype p2 = p1.Clone() as Prototype;
p2.Detail.Height = "200";
Console.WriteLine("<This is Deep Copy>");
Console.WriteLine(p1.ToString());
Console.WriteLine(p2.ToString());
Prototype p3 = new Prototype { Name = "Vahid", Health = "OK", Detail = new AdditionalDetails { Height = "100" } };
Prototype p4 = p3.ShallowClone() as Prototype;
p4.Detail.Height = "200";
Console.WriteLine("\n<This is Shallow Copy>");
Console.WriteLine(p3.ToString());
Console.WriteLine(p4.ToString());