示例#1
0
        public void CreateConcreteProductATest()
        {
            Creator creator = new ConcreteCreatorA();

            Assert.That(() => creator.AnOperation(),
                        Throws.Exception.With.Message.EqualTo("AnOperation with object FactoryMethod.Application.ConcreteProductA type"));
        }
示例#2
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();
        }
示例#4
0
        static void Main(string[] args)
        {
            var creator = new ConcreteCreatorA();
            var product = creator.CreateProduct();
            Console.WriteLine("Created {0}", product.ToString());

            Console.ReadKey();
        }
示例#5
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);
        }
示例#6
0
        private void RunFactoryMethodTest()
        {
            Creator creator = new ConcreteCreatorA();

            FactoryMethodPattern.Product Item1 = creator.FactoryMethod();

            creator = new ConcreteCreatorB();
            FactoryMethodPattern.Product Item2 = creator.FactoryMethod();
        }
示例#7
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);
     }
 }
示例#8
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();
        }
示例#11
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();
        }
示例#12
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();
        }
示例#13
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();
            }
示例#14
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);
            }
        }
示例#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);
            }
        }
示例#16
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);
            }
        }
示例#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());
            }
        }
        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));
            }
        }
示例#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
        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();
        }
示例#21
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();
        }
    }
示例#22
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();
        }
示例#23
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();
                }
示例#24
0
        // Factory -> creational pattern that uses a spezialized object to create other obj-s, abstracting the use of concrete objects.
        public void FactoryClient()
        {
            Creator creator = new ConcreteCreatorA();

            Creational.Factory.Product product = creator.FactoryMethod();
        }