/// <summary>
        /// 浅克隆
        /// </summary>
        /// <returns></returns>
        internal override AbstractPrototype ShallowClone()
        {
            AbstractPrototype cp = new ConcretePrototype();

            cp.entity = this.entity;
            return(cp);
        }
예제 #2
0
        static void Main()
        {
            ConcretePrototype prototype = new ConcretePrototype("1");
            ConcretePrototype clone     = (ConcretePrototype)prototype.Clone();

            Console.WriteLine("Cloned: {0}", clone.Id);
        }
예제 #3
0
        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");
        }
예제 #4
0
        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();
        }
예제 #5
0
        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);
        }
예제 #6
0
        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();
        }
예제 #7
0
        static void Main()
        {
            // 孙悟空原型
            MonkeyKingPrototype monkeyKingPrototype = new ConcretePrototype("MonkeyKing");

            // 变一个
            MonkeyKingPrototype cloneMonkeyKing1 = monkeyKingPrototype.Clone() as ConcretePrototype;

            // 变两个
            MonkeyKingPrototype cloneMonkeyKing2 = monkeyKingPrototype.Clone() as ConcretePrototype;
        }
예제 #8
0
        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();
        }
예제 #9
0
        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();
        }
예제 #10
0
        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 패턴을 결합해서 사용하면 좋습니다.
        }
예제 #11
0
        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));
        }
예제 #12
0
        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
        }
예제 #13
0
        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
        }
예제 #14
0
        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));
        }
예제 #15
0
        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();
        }
예제 #16
0
 static void Main(string[] args)
 {
     PrototypeClass prototype       = new ConcretePrototype("X-One");
     PrototypeClass clonedPrototype = prototype.Clone();
 }
예제 #17
0
 /// <summary>
 /// 浅克隆
 /// </summary>
 /// <returns></returns>
 internal override AbstractPrototype ShallowClone()
 {
     AbstractPrototype cp = new ConcretePrototype();
     cp.entity = this.entity;
     return cp;
 }
예제 #18
0
 static void Main()
 {
     ConcretePrototype prototype = new ConcretePrototype("1");
     ConcretePrototype clone = (ConcretePrototype)prototype.Clone();
     Console.WriteLine("Cloned: {0}", clone.Id);
 }
예제 #19
0
 public ConcretePrototype(ConcretePrototype other)
 {
     Console.WriteLine("ConcretePrototype has been created.");
     Value = other.Value;
 }