コード例 #1
0
        /// <summary>
        /// 原型模式-浅拷贝
        /// </summary>
        static void Func2()
        {
            Sheep sheep = new Sheep
            {
                Name  = "DuoLi",
                Age   = 10,
                Color = new SheepColor {
                    Code = "001", Name = "White"
                }
            };

            Sheep sheep1 = (Sheep)sheep.Clone();
            Sheep sheep2 = (Sheep)sheep.Clone();

            MyPrint(sheep);
            MyPrint(sheep1);
            MyPrint(sheep2);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var jolly = new Sheep("Jolly", "Mountain Sheep");
            var dolly = jolly.Clone();

            Console.WriteLine(dolly.Type);
            Console.WriteLine("==========");

            dolly.Name = "Dolly";
            dolly.Type = "Clone Sheep";

            Console.WriteLine(dolly.Name);
            Console.WriteLine(dolly.Type);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var original = new Sheep("Jolly", "Mountain Sheep");

            Console.WriteLine(original.Name);             // Jolly
            Console.WriteLine(original.Category);         // Mountain Sheep

            var cloned = original.Clone();

            cloned.Name = "Dolly";
            Console.WriteLine(cloned.Name);             // Dolly
            Console.WriteLine(cloned.Category);         // Mountain Sheep
            Console.WriteLine(original.Name);           // Jolly
            Console.Read();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Jac21/DesignPatterns
        static void Main()
        {
            // When an object is required that is similar to existing object or
            // when the creation would be expensive as compared to cloning.

            var original = new Sheep("Jeremy", "Not a sheep, yo");

            Console.WriteLine($"Name: {original.Name}");
            Console.WriteLine($"Category: {original.Category}");

            var cloned = original.Clone();

            cloned.Name = "Dolly";
            Console.WriteLine($"Name: {cloned.Name}");
            Console.WriteLine($"Category: {cloned.Category}");
            Console.WriteLine($"Name: {original.Name}");
        }