/// <summary> /// 浅克隆 /// </summary> /// <returns></returns> internal override AbstractPrototype ShallowClone() { AbstractPrototype cp = new ConcretePrototype(); cp.entity = this.entity; return(cp); }
static void Main() { ConcretePrototype prototype = new ConcretePrototype("1"); ConcretePrototype clone = (ConcretePrototype)prototype.Clone(); Console.WriteLine("Cloned: {0}", clone.Id); }
static void Main(string[] args) { Prototype prototype = new ConcretePrototype(1); Prototype clone = prototype.Clone(); System.Console.WriteLine(prototype.Id + " proto"); System.Console.WriteLine(clone.Id + " Clone"); }
static void Main(string[] args) { var state = new ReferenceType(1, "Some string"); var prototype = new ConcretePrototype(state); var clone = prototype.Clone(); var dotNetPrototype = new DotNetPrototype(state); var dotNetClone = dotNetPrototype.Clone(); }
public override Prototype DeepClone() { //先浅拷贝 ConcretePrototype p = (ConcretePrototype)this.MemberwiseClone(); //再处理引用类型成员新建处理。 以当前成员新new出来,或 在该引用类型中也实现深拷贝方法,然后调用。 p.extra = new Extra(this.id, this.name); //这句其实可以不需要,因为虽然String不是基础成员,但因为其为静态常量,所以具有基础成员的性质。 p.name = String.Copy(this.name); return(p); }
static void Main(string[] args) { Prototype prototype = new ConcretePrototype("yepeng"); Prototype clone = prototype.Clone() as ConcretePrototype; Prototype clone2 = prototype.Clone() as ConcretePrototype; Console.WriteLine($"clone1:{clone.Id},clone2:{clone2.Id}"); Console.ReadKey(); }
static void Main() { // 孙悟空原型 MonkeyKingPrototype monkeyKingPrototype = new ConcretePrototype("MonkeyKing"); // 变一个 MonkeyKingPrototype cloneMonkeyKing1 = monkeyKingPrototype.Clone() as ConcretePrototype; // 变两个 MonkeyKingPrototype cloneMonkeyKing2 = monkeyKingPrototype.Clone() as ConcretePrototype; }
static void Main(string[] args) { var abstractPrototype = new ConcretePrototype(System.Guid.NewGuid().ToString()); var cloneType = abstractPrototype.Clone() as ConcretePrototype; Console.WriteLine("Cloned1:\t" + cloneType.Id); var cloneType2 = abstractPrototype.Clone() as ConcretePrototype; Console.WriteLine("Cloned2:\t" + cloneType.Id); Console.Read(); }
static void Main(string[] args) { ConcretePrototype p1 = new ConcretePrototype("I"); ConcretePrototype c1 = (ConcretePrototype)p1.Clone(); Console.WriteLine("Cloned: {0}", c1.Id); ConcretePrototype1 p2 = new ConcretePrototype1("II"); ConcretePrototype1 c2 = (ConcretePrototype1)p2.Clone(); Console.WriteLine("Cloned: {0}", c2.Id); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { //.Net에서는 ICloneable interface를 이용해서 Prototype 패턴을 구현합니다. var prototype = new ConcretePrototype(); var prototype2 = new ConcretePrototype(prototype); var clone = prototype.Clone(); prototype.print(); prototype2.print(); //가장 상위의 Prototype class에는 print() 메소드가 없습니다. Console.WriteLine($"Clone Value: {clone.Value}"); //Composite 패턴과 Decorator 패턴을 결합해서 사용하면 좋습니다. }
public void Execute() { //浅克隆:创建新的对象,指向老对象的值 //深克隆: 创建新的对象,指向新创建的老对象的值 Console.WriteLine("请选择克隆方式:1.浅克隆 2.深克隆"); string cloneType = Console.ReadLine(); AbstractPrototype prototype,copy; prototype = new ConcretePrototype(); if (cloneType=="1") copy=prototype.ShallowClone(); else copy = prototype.DeepClone(); Console.WriteLine("对象是否相同" + (prototype == copy)); Console.WriteLine("值是否相同" + (prototype.entity == copy.entity)); }
static private void DeepCloneTest() { //创建原型 a, 并浅拷贝为 b。 ConcretePrototype a = new ConcretePrototype(); ConcretePrototype b = (ConcretePrototype)a.DeepClone(); Debug.Log(a.id + ", " + a.name + ", " + a.extra.id + ", " + a.extra.name); //1, A, 1, A Debug.Log(b.id + ", " + b.name + ", " + b.extra.id + ", " + b.extra.name); //1, A, 1, A //尝试修改b b.id = 2; b.name = "B"; b.extra.id = 2; b.extra.name = "B"; //B正常,全部被修改。 //A正常,没有因为B的修改而被修改。 Debug.Log(a.id + ", " + a.name + ", " + a.extra.id + ", " + a.extra.name); //1, A, 1, A Debug.Log(b.id + ", " + b.name + ", " + b.extra.id + ", " + b.extra.name); //2, B, 2, B }
static private void ShallowCloneTest() { //创建原型 a, 并浅拷贝为 b。 ConcretePrototype a = new ConcretePrototype(); ConcretePrototype b = (ConcretePrototype)a.ShallowClone(); Debug.Log(a.id + ", " + a.name + ", " + a.extra.id + ", " + a.extra.name); //1, A, 1, A Debug.Log(b.id + ", " + b.name + ", " + b.extra.id + ", " + b.extra.name); //1, A, 1, A //尝试修改b b.id = 2; b.name = "B"; b.extra.id = 2; b.extra.name = "B"; //B正常,全部被修改 //A的基础类型成员(string不是基础成员,但具有基础成员的性质)没有被修改(正常)。但引用类型成员被修改(因为浅拷贝的缘故) Debug.Log(a.id + ", " + a.name + ", " + a.extra.id + ", " + a.extra.name); //1, A, 2, B Debug.Log(b.id + ", " + b.name + ", " + b.extra.id + ", " + b.extra.name); //2, B, 2, B }
public void Execute() { //浅克隆:创建新的对象,指向老对象的值 //深克隆: 创建新的对象,指向新创建的老对象的值 Console.WriteLine("请选择克隆方式:1.浅克隆 2.深克隆"); string cloneType = Console.ReadLine(); AbstractPrototype prototype, copy; prototype = new ConcretePrototype(); if (cloneType == "1") { copy = prototype.ShallowClone(); } else { copy = prototype.DeepClone(); } Console.WriteLine("对象是否相同" + (prototype == copy)); Console.WriteLine("值是否相同" + (prototype.entity == copy.entity)); }
static void Main(string[] args) { Console.WriteLine("Prototype example"); Prototype first = new ConcretePrototype { Id = 1, Description = new PrototypeDescription { Name = "test name", Details = "test details" } }; Console.WriteLine($"First:{first}"); Prototype second = first.ShallowCopy(); second.Id = 2; second.Description.Details = "new details"; Console.ReadKey(); Console.WriteLine($"Second:{second}"); Console.ReadKey(); Console.WriteLine($"First:{first}"); Prototype third = first.DeepCopy(); third.Id = 3; third.Description.Details = "very new details"; Console.ReadKey(); Console.WriteLine($"Third:{third}"); Console.ReadKey(); Console.WriteLine($"First:{first}"); Console.ReadKey(); }
static void Main(string[] args) { PrototypeClass prototype = new ConcretePrototype("X-One"); PrototypeClass clonedPrototype = prototype.Clone(); }
/// <summary> /// 浅克隆 /// </summary> /// <returns></returns> internal override AbstractPrototype ShallowClone() { AbstractPrototype cp = new ConcretePrototype(); cp.entity = this.entity; return cp; }
public ConcretePrototype(ConcretePrototype other) { Console.WriteLine("ConcretePrototype has been created."); Value = other.Value; }