Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            #region 结构实现
            Factory[] factories = new Factory[2];
            factories[0] = new ConcreteFactoryA();
            factories[1] = new ConcreteFactoryB();

            foreach (Factory factory in factories)
            {
                Product product = factory.CreateProduct();
                Console.WriteLine("Created {0}", product.GetType().Name);
            }
            #endregion

            Console.WriteLine("******************************");

            #region 实践应用
            // 定义一个鸡腿工厂
            IKFCFactory kfcFactory = new ChickenFactory();
            // 生产鸡腿
            KFCFood food1 = kfcFactory.CreateFood();
            food1.Display();
            kfcFactory = new WingsFactory();
            // 生产鸡翅
            KFCFood food2 = kfcFactory.CreateFood();
            food2.Display();
            #endregion

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Factory factory  = new ConcreteFactoryA();
            Product _product = factory.GetProduct();

            Console.WriteLine(_product.ToString());

            factory  = new ConcreteFactoryB();
            _product = factory.GetProduct();
            Console.WriteLine(_product.ToString());
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            AbstractFactory factory;
            AbstractProduct product;

            factory = new ConcreteFactoryA();
            product = factory.FactoryMethod();

            factory = new ConcreteFactoryB();
            product = factory.FactoryMethod();

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            IFactory factoryA = new ConcreteFactoryA();

            Console.WriteLine(factoryA.CreateProduct().GetType().Name);

            IFactory factoryB = new ConcreteFactoryB();

            Console.WriteLine(factoryB.CreateProduct().GetType().Name);

            IFactory factoryC = new ConcreteFactoryC();

            Console.WriteLine(factoryB.CreateProduct().GetType().Name);
        }