예제 #1
0
        static void FactoryTester()
        {
            #region sample 1
            // use of factory creational pattern
            var creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products
            foreach (var creator in creators)
            {
                var product = creator.FactoryMethod();
                Console.WriteLine("Created {0}", product.GetType().Name);
            }
            #endregion

            #region sample 2
            // Note: constructors call Factory Method
            var documents = new Document[2];

            documents[0] = new Resume();
            documents[1] = new Report();

            // Display document pages
            foreach (var document in documents)
            {
                Console.WriteLine("\n" + document.GetType().Name + "--");
                foreach (var page in document.Pages)
                {
                    Console.WriteLine(" " + page.GetType().Name);
                }
            }
            #endregion
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Structural example:");
            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}", product.GetType().Name);
            }

            Console.WriteLine();

            Console.WriteLine("Real-world example:");
            Document[] documents = new Document[2];

            documents[0] = new Resume();
            documents[1] = new Report();

            foreach (Document document in documents)
            {
                Console.WriteLine("\n" + document.GetType().Name + "--");
                foreach (Page page in document.Pages)
                {
                    Console.WriteLine(" " + page.GetType().Name);
                }
            }

            Console.ReadKey();
        }
예제 #3
0
        public void CreateConcreteProductBTest()
        {
            Creator creator = new ConcreteCreatorB();

            Assert.That(() => creator.AnOperation(),
                        Throws.Exception.With.Message.EqualTo("AnOperation with object FactoryMethod.Application.ConcreteProductB type"));
        }
예제 #4
0
        public void TestConcreteCreator()
        {
            var creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();
            Assert.AreNotEqual(creators[0].FactoryMethod().GetType().Name, creators[1].FactoryMethod().GetType().Name);
            Assert.AreSame(creators[0].FactoryMethod().GetType().BaseType, creators[1].FactoryMethod().GetType().BaseType);
        }
예제 #5
0
        private void RunFactoryMethodTest()
        {
            Creator creator = new ConcreteCreatorA();

            FactoryMethodPattern.Product Item1 = creator.FactoryMethod();

            creator = new ConcreteCreatorB();
            FactoryMethodPattern.Product Item2 = creator.FactoryMethod();
        }
예제 #6
0
 public override void run()
 {
     Creator[] creators = new Creator[2];
     creators[0] = new ConcreteCreatorA();
     creators[1] = new ConcreteCreatorB();
     foreach (Creator creator in creators)
     {
         Product product = creator.FactoryMethod();
         Console.WriteLine("Created {0}",
                           product.GetType().Name);
     }
 }
예제 #7
0
    public static void MainDemo(string[] args)
    {
        // FactoryMethod returns ProductA
        Creator c = new ConcreteCreatorA();
        Product p = c.FactoryMethod();
        Console.WriteLine("Created {0}", p);

        // FactoryMethod returns ProductB
        c = new ConcreteCreatorB();
        p = c.FactoryMethod();
        Console.WriteLine("Created {0}", p);

    }
        public static void Run()
        {
            Console.WriteLine("\nFactory Method Structural Practice");
            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}", product.GetType().Name);
            }
        }
        static void Main(string[] args)
        {
            AbstractCreator creatorA = new ConcreteCreatorA();
            IProduct        productA = creatorA.FactoryMethod();

            productA.Launch();

            AbstractCreator creatorB = new ConcreteCreatorB();
            IProduct        productB = creatorB.FactoryMethod();

            productB.Launch();

            Console.Read();
        }
예제 #10
0
        public static void Main(string[] args)
        {
            Creator[] creators = new Creator[2];
            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            foreach(Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}", product.GetType().Name);
            }

            Console.Read();
        }
예제 #11
0
        static void TestSample()
        {
            var creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            foreach (var creator in creators)
            {
                Product product = creator.FactoryMethod();
                Debug.WriteLine(creator.GetType().Name + " has created: " + product.GetType().Name);
            }
            Console.ReadKey();
        }
예제 #12
0
            private static void Test()
            {
                var creators = new Creator[2];

                creators[0] = new ConcreteCreatorA();
                creators[1] = new ConcreteCreatorB();

                foreach (var creator in creators)
                {
                    var product = creator.FactoryMethod();
                    Console.WriteLine("Created {0}", product.GetType().Name);
                }

                Console.ReadKey();
            }
예제 #13
0
        public static void Demo()
        {
            // An array of creators
            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products
            foreach (Creator creator in creators)
            {
                IProduct product = creator.FactoryMethod();
                Console.WriteLine("Created {0}",
                  product.GetType().Name);
            }
        }
예제 #14
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        public static void Test()
        {
            // An array of creators
            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products
            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}",
                                  product.GetType().Name);
            }
        }
예제 #15
0
        static void RunFactoryMethod()
        {
            // 工厂方法生成的产品一般是单一
            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products
            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}",
                                  product.GetType().Name);
            }
        }
        public void FactoryTest()
        {
            // An array of creators

            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products

            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Utility.LogEntry(string.Format("Created {0}", product.GetType().Name));
            }
        }
예제 #17
0
        static void Main(string[] args)
        {
            Creator creatorOne = new ConcreteCreatorA();
            Creator creatorTwo = new ConcreteCreatorB();

            // Should Print Types of ABD
            foreach (Product p in creatorOne.Products)
            {
                Console.WriteLine(p.GetType().ToString());
            }

            // Shoudl Print Types of CD
            foreach (Product p in creatorTwo.Products)
            {
                Console.WriteLine(p.GetType().ToString());
            }
        }
예제 #18
0
        internal static void Run()
        {
            Creator[] creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products

            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}",
                                  product.GetType().Name);
            }

            Console.ReadKey();
        }
예제 #19
0
    // Entry point into console application.
    static void Main()
    {
        // An array of creators
        Creator[] creators = new Creator[2];

        creators[0] = new ConcreteCreatorA();
        creators[1] = new ConcreteCreatorB();

        // Iterate over creators and create products
        foreach (Creator creator in creators)
        {
            Product product = creator.FactoryMethod();
            Console.WriteLine("Created {0}", product.GetType().Name);
        }

        // Wait for user
        Console.ReadKey();
    }
예제 #20
0
    static void Main(string [] args)
    {
        Creater creater;

        Console.WriteLine("Choose product A OR B");
        string action = Console.ReadLine();

        if (action == "A")
        {
            creater = new ConcreteCreatorA();
            creater.createProduct();
        }
        if (action == "B")
        {
            creater = new ConcreteCreatorB();
            creater.createProduct();
        }
    }
예제 #21
0
        // "Product"

        // "ConcreteProductA"
        public void TestFactoryMethodPattern()
        {
            // An array of creators
            var creators = new Creator[2];

            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            // Iterate over creators and create products
            foreach (Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created {0}",
                                  product.GetType().Name);
            }

            // Wait for user
            Console.Read();
        }
예제 #22
0
                static void Main(string[] args)
                {
                    Creator[] creators = new Creator[2];
                    creators[0] = new ConcreteCreatorA();
                    creators[1] = new ConcreteCreatorB();

                    foreach (Creator obj in creators)
                    {
                        Product product = obj.FactoryMethod();
                        Console.WriteLine("Created {0}", product.GetType().Name);
                    }
                    ConcreteCreatorA cc1 = new ConcreteCreatorA();

                    cc1.Method1();
                    ConcreteCreatorB cc2 = new ConcreteCreatorB();

                    cc2.Method2();
                    Console.ReadKey();
                }