Esempio n. 1
0
        /// <summary>
        /// 原型模式
        /// </summary>
        static void TestProtoType()
        {
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 p2 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned:{0}", p2.Id);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ConcretePrototype1 cp1  = new ConcretePrototype1();
            ConcretePrototype1 c1_1 = (ConcretePrototype1)cp1.Clone();

            Console.WriteLine(c1_1.SomeMethod);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Structural example:");
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned: {0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("II");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

            Console.WriteLine("Cloned: {0}", c2.Id);

            Console.WriteLine();

            Console.WriteLine("Real-world example:");
            ColorManager colormanager = new ColorManager();

            colormanager["red"]   = new Color(255, 0, 0);
            colormanager["green"] = new Color(0, 255, 0);
            colormanager["blue"]  = new Color(0, 0, 255);

            colormanager["angry"] = new Color(255, 54, 0);
            colormanager["peace"] = new Color(128, 211, 128);
            colormanager["flame"] = new Color(211, 34, 20);

            Color color1 = colormanager["red"].Clone() as Color;
            Color color2 = colormanager["peace"].Clone() as Color;
            Color color3 = colormanager["flame"].Clone() as Color;

            Console.ReadKey();
        }
Esempio n. 4
0
        static void PrototypeTester()
        {
            #region sample 1
            // Create two instances and clone each

            var p1 = new ConcretePrototype1("I");
            var c1 = (ConcretePrototype1)p1.Clone();
            Console.WriteLine("Cloned: {0}", c1.Id);

            var p2 = new ConcretePrototype2("II");
            var c2 = (ConcretePrototype2)p2.Clone();
            Console.WriteLine("Cloned: {0}", c2.Id);
            #endregion

            #region sample 2
            var colormanager = new ColorManager();

            // Initialize with standard colors
            colormanager["red"]   = new Color(255, 0, 0);
            colormanager["green"] = new Color(0, 255, 0);
            colormanager["blue"]  = new Color(0, 0, 255);

            // User adds personalized colors
            colormanager["angry"] = new Color(255, 54, 0);
            colormanager["peace"] = new Color(128, 211, 128);
            colormanager["flame"] = new Color(211, 34, 20);

            // User clones selected colors
            var color1 = colormanager["red"].Clone() as Color;
            var color2 = colormanager["peace"].Clone() as Color;
            var color3 = colormanager["flame"].Clone() as Color;
            #endregion
        }
Esempio n. 5
0
        void Operation()
        {
            Prototype prototype = new ConcretePrototype1(1);
            Prototype clone     = prototype.Clone();

            prototype = new ConcretePrototype2(2);
            clone     = prototype.Clone();
        }
Esempio n. 6
0
        public static void Prototype()
        {
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned:{0}", c1.Id);
            Console.Read();
        }
Esempio n. 7
0
        public void ExecuteSwallowClone()
        {
            //Shallow Clone
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 p2 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine($"Cloned:{p2.ID}");
        }
Esempio n. 8
0
        public void Operation()
        {
            Prototype prototype1 = new ConcretePrototype1(1);
            Prototype clone1     = prototype1.Clone();

            Prototype prototype2 = new ConcretePrototype2(2);
            Prototype clone2     = prototype2.Clone();
        }
Esempio n. 9
0
        public static void Display()
        {
            Prototype prototype = new ConcretePrototype1(1);
            Prototype clone     = prototype.Clone();

            prototype = new ConcretePrototype2(2);
            clone     = prototype.Clone();
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            ConcretePrototype1 pa = new ConcretePrototype1("I");
            var pa1 = (Prototype)pa.Clone();

            Console.WriteLine("Hello World!");
            A a = new A("fdf");
        }
Esempio n. 11
0
        public void CreatePrototype1()
        {
            Prototype proto1     = new ConcretePrototype1("Prototype 1");
            Prototype proto1Copy = (Prototype)proto1.Clone();

            Assert.IsInstanceOfType(proto1Copy, typeof(ConcretePrototype1));
            Assert.AreEqual(proto1.PrototypeInfo, proto1Copy.PrototypeInfo);
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            Prototype obj   = new ConcretePrototype1("1");
            Prototype clone = obj.Clone();

            obj   = new ConcretePrototype2("2");
            clone = obj.Clone();
        }
Esempio n. 13
0
        public void PrototypeTest()
        {
            var prototype = new ConcretePrototype1("1", 2);

            Console.WriteLine($"{prototype.Id}{prototype.IntId}");
            var clone = prototype.Clone();

            Console.WriteLine($"{clone.Id}{clone.IntId}");
        }
Esempio n. 14
0
        public void Create()
        {
            var p1 = new ConcretePrototype1("1");
            var p2 = new ConcretePrototype2("2", 123);

            // Clone the objects.
            var c1 = p1.Clone();
            var c2 = p2.Clone();
        }
Esempio n. 15
0
    public PrototypeClientA()
    {
        ConcretePrototype1 p1 = new ConcretePrototype1("I");
        ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
        Console.WriteLine("Cloned: {0}", c1.Id);

        ConcretePrototype2 p2 = new ConcretePrototype2("II");
        ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
        Console.WriteLine("Cloned: {0}", c2.Id);
    }
Esempio n. 16
0
        // Prototype -> allows cloning of objects on the base of already created objects-prototypes. Gives technique for cloning
        // use when concrete type of new object has to be defined dynamically; when cloning is preferable to initialization;
        public void ClientPrototype()
        {
            Prototype prototype = new ConcretePrototype1(1);
            Prototype clone     = prototype.Clone();

            Console.WriteLine(clone.Id);
            prototype = new ConcretePrototype2(3);
            clone     = prototype.Clone();
            Console.WriteLine(clone.Id);
        }
Esempio n. 17
0
        static void Main(string[] args)
        {
            Prototype prototype = null;
            Prototype clone     = null;

            prototype = new ConcretePrototype1(1);
            clone     = prototype.Clone();

            prototype = new ConcretePrototype2(2);
            clone     = prototype.Clone();
        }
Esempio n. 18
0
        [Test] public void TestClone()
        {
            var p1 = new ConcretePrototype1("I");
            var c1 = (ConcretePrototype1)p1.Clone();

            Assert.AreEqual(p1.Id, c1.Id);
            var p2 = new ConcretePrototype2("II");
            var c2 = (ConcretePrototype2)p2.Clone();

            Assert.AreEqual(p2.Id, c2.Id);
        }
Esempio n. 19
0
        public static void Used()
        {
            var obj1 = new ConcretePrototype1();

            //需要大量相同对象时 可以使用深复制
            ConcretePrototype1[] objs = new ConcretePrototype1[100];
            for (int i = 0; i < objs.Length; i++)
            {
                objs[0] = (ConcretePrototype1)obj1.Clone();
            }
        }
        private static void FirstDemo()
        {
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned: {0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("II");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

            Console.WriteLine("Cloned: {0}", c2.Id);
        }
Esempio n. 21
0
        static void Prototype_Exemplo1()
        {
            var p1 = new ConcretePrototype1("I");
            var c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned: {0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("II");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

            Console.WriteLine("Cloned: {0}", c2.Id);
        }
Esempio n. 22
0
        public static void Run()
        {
            ConcretePrototype1 p1 = new ConcretePrototype1("i");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("cloned: {0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("ii");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

            Console.WriteLine("cloned: {0}", c2.Id);
        }
Esempio n. 23
0
  public static void Main( string[] args )
  {
    // Create two instances and clone each
    ConcretePrototype1 p1 = new ConcretePrototype1("I");
    ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
    Console.WriteLine( "Cloned: {0}", c1.Id );

    ConcretePrototype2 p2 = new ConcretePrototype2("II");
    ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
    Console.WriteLine( "Cloned: {0}", c2.Id );

  }
Esempio n. 24
0
        static void Main(string[] args)
        {
            CreateSection("Singletons");

            Singleton s1 = Singleton.Instance();
            Singleton s2 = Singleton.Instance();

            if (s1 == s2)
            {
                Console.WriteLine("Objects are the same instance.");
            }
            EndSection();

            CreateSection("Prototypes");

            var p1 = new ConcretePrototype1(1);

            Console.WriteLine("Original ID: {0}", p1.Id);

            var c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned ID: {0}", c1.Id);

            EndSection();

            CreateSection("Factory");

            var creators = new Creator[2];

            creators[0] = new ConcreteCreator();
            creators[1] = new ConcreteCreator2();

            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();

                Console.WriteLine("Created {0}", product.GetType().Name);
            }

            EndSection();

            CreateSection("Observer pattern");

            var bookSubject = new BookSubject("Gone Girl", "True Crime");

            bookSubject.Attach(new ConcreteObserver("The Librarian"));

            bookSubject.SubjectType = "Horror";
            bookSubject.SubjectType = "Romance";

            Console.ReadKey();
        }
Esempio n. 25
0
        public void DemoPrototypePattern()
        {
            Console.WriteLine("-----------------Prototype Pattern Example-----------------");
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned: {0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("II");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

            Console.WriteLine("Cloned: {0}", c2.Id);
        }
Esempio n. 26
0
        static void Main(string[] args)
        {
            ConcretePrototype1 p1 = new ConcretePrototype1("I");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            p1.Numbers[0] = 10;
            c1.Numbers[0] = 100;
            Console.WriteLine("Cloned:{0},{1},{2},{3}", p1.Id, c1.Id, p1.Numbers[0], c1.Numbers[0]);

            c1.Id = "C";
            Console.WriteLine("Cloned:{0},{1}", p1.Id, c1.Id);
            Console.ReadKey();
        }
Esempio n. 27
0
    void Start( )
    {
        // Create two instances and clone each

        ConcretePrototype1 p1 = new ConcretePrototype1("I");
        ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

        Debug.Log("Cloned: " + c1.Id);

        ConcretePrototype2 p2 = new ConcretePrototype2("II");
        ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

        Debug.Log("Cloned: " + c2.Id);
    }
Esempio n. 28
0
        public static void PrototypeStructural()
        {
            // Create two instances and clone each

            var p1 = new ConcretePrototype1("I");
            var c1 = (ConcretePrototype1)p1.Clone();

            Log.WriteLine("Cloned: {0}", c1.Id);

            var p2 = new ConcretePrototype2("II");
            var c2 = (ConcretePrototype2)p2.Clone();

            Log.WriteLine("Cloned: {0}", c2.Id);
        }
Esempio n. 29
0
            private static void Test()
            {
                var p1 = new ConcretePrototype1("I");
                var c1 = (ConcretePrototype1)p1.Clone();

                Console.WriteLine("Cloned: {0}", c1.Id);

                var p2 = new ConcretePrototype2("II");
                var c2 = (ConcretePrototype2)p2.Clone();

                Console.WriteLine("Cloned: {0}", c2.Id);

                Console.ReadKey();
            }
Esempio n. 30
0
        static bool TestPrototypePattern()
        {
            Console.WriteLine("TESTING THE PROTOTYPE PATTERN: ");

            var prototype = new ConcretePrototype1
            {
                Property1        = "A",
                Property2        = "B",
                PrototypeDetails = new PrototypeDetails {
                    Details = "'prototype' details"
                }
            };

            //New instance using the prototype
            var NewObject = prototype.Clone() as ConcretePrototype1;

            //Without using Deep copy, the next line whould change the same PrototypeDetails instance
            //used in 'prototype' and 'NewObject', since both would be using the same reference to this object instance
            NewObject.PrototypeDetails.Details = "New details for 'NewObject'";

            Console.WriteLine(prototype);
            Console.WriteLine(NewObject);
            //OUTPUT
            //Property1: A Property2:B PrototypeDetails:"'prototype' details"
            //Property1: A Property2:B PrototypeDetails:"New details for 'NewObject'"

            var prototype2 = new ConcretePrototype2
            {
                Property1        = "X",
                Property2        = "Y",
                OtherProperty    = "Z",
                PrototypeDetails = new PrototypeDetails {
                    Details = "'prototype2' details"
                }
            };

            //New instance using the prototype2
            var NewObject2 = prototype2.Clone() as ConcretePrototype2;

            NewObject2.PrototypeDetails.Details = "New details for 'NewObject2'";

            Console.WriteLine(prototype2);
            Console.WriteLine(NewObject2);
            //OUTPUT
            //Property1: X Property2:Y PrototypeDetails:"'prototype2' details" OtherProperty: Z
            //Property1:X Property2:Y PrototypeDetails:"New details for 'NewObject2'" OtherProperty: Z

            return(true);
        }
Esempio n. 31
0
        //Прототип - Prototype
        public Run Prototype()
        {
            Console.WriteLine("\nPrototype:");

            // Create two instances and clone each
            Patterns.Creational.Prototype.Prototype p1 = new ConcretePrototype1("I");
            Patterns.Creational.Prototype.Prototype c1 = p1.Clone();
            Console.WriteLine("Cloned: {0}", c1.Id);

            Patterns.Creational.Prototype.Prototype p2 = new ConcretePrototype2("II");
            Patterns.Creational.Prototype.Prototype c2 = p2.Clone();
            Console.WriteLine("Cloned: {0}", c2.Id);

            return(this);
        }
Esempio n. 32
0
    // Entry point into console application.
    static void Main()
    {
        // Create two instances and clone each

        ConcretePrototype1 p1 = new ConcretePrototype1("I");
        ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
        Console.WriteLine("Cloned: {0}", c1.Id);

        ConcretePrototype2 p2 = new ConcretePrototype2("II");
        ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
        Console.WriteLine("Cloned: {0}", c2.Id);

        // Wait for user
        Console.ReadKey();
    }
Esempio n. 33
0
            public void shadow_cloning()
            {
                Prototype prototype = new ConcretePrototype1(nam_clon);

                if (nam_clon != max_clon)
                {
                    Console.WriteLine("я наруто утзумаки и теперь меня на 1 больше");
                    Prototype clone = prototype.Clone();
                    nam_clon++;
                }
                else
                {
                    Console.WriteLine("я наруто утзумаки и я стану хокаге");
                }
            }