Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input numberA:");
            double numA = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please input numberB:");
            double numB = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please input algorithm:");
            string algorithm = Console.ReadLine();

            Operation operation;

            SimpleFactory sf = new SimpleFactory();

            operation = sf.createOperation(algorithm);
            double result = 0d;

            if (operation != null)
            {
                result = operation.getResult(numA, numB);
            }
            else
            {
                Console.WriteLine("WRONG MESSAGE INPUT!");
            }


            Console.WriteLine("Result:{0}", result);
            Console.Read();
        }
Example #2
0
        static void Main(string[] args)
        {
            SimpleFactory simpleFactory = new SimpleFactory();

            Product productA = simpleFactory.CreateProduct("ProductA");

            productA.Operation();

            Product productB = simpleFactory.CreateProduct("ProductB");

            productB.Operation();
        }
        private static void TestSimpleFactory()
        {
            //使用简单工厂,客户直接去点菜就可以(自己不用操心如何做菜)
            //后续扩展直接实现抽象类就可以,低耦合,易扩展
            Food food1 = SimpleFactory.MakeFood(FoodType.Patato);

            food1.Make();
            Food food2 = SimpleFactory.MakeFood(FoodType.Chicken);

            food2.Make();
            Food food3 = SimpleFactory.MakeFood(FoodType.TomatoWithEggs);

            food3.Make();
        }
Example #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("*** Simple Factory Pattern Demo***\n");
            IAnimal        preferredType = null;
            ISimpleFactory simpleFactory = new SimpleFactory();

            #region The code region that will vary based on users preference
            preferredType = simpleFactory.CreateAnimal();
            #endregion

            #region The codes that do not change frequently
            preferredType.Speak();
            preferredType.Action();
            #endregion

            Console.ReadKey();
        }
Example #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Simple Factory Pattern");
            IAnimal preferredType = null;

            SimpleFactory simpleFactory = new SimpleFactory();

            #region The code region that can vary based on users preference

            //since this part may vary, we're moving the part to CreateAnimal() in SimpleFactory class

            preferredType = simpleFactory.CreateAnimal();

            #endregion

            #region The code that do not change frequently

            preferredType.AboutMe();

            #endregion


            Console.ReadLine();
        }