zoukankan      html  css  js  c++  java
  • 三种工厂模式的比较

    1.简单工厂模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _1.简单工厂模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Food meat = FactoryFood.Cook("Meat");
                Food vegetable = FactoryFood.Cook("vegetable");
                meat.print();
                vegetable.print();
                Console.ReadLine();
            }
        }
        public interface Food
        {
            void print();
        }
    
        public class Meat : Food
        {
            public void print()
            {
                Console.WriteLine("Meat");
            }
        }
    
        public class vegetable : Food
        {
            public void print()
            {
                Console.WriteLine("vegetable");
            }
        }
    
        public class FactoryFood
        {
       
            public static Food Cook(string Name)
            {
                 Food food = null;
    
                if (Name == "Meat")
                {
                    food = new Meat();
                }
                else if (Name == "vegetable")
                {
                    food = new vegetable();
                }
                return food;
            }
        }
    }

    2.工厂方法模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _2.工厂方法模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Factory meatfactory = new MeatFactory();
                Factory vegetablefactory = new VegetableFactory();
                Food meat = meatfactory.create();
                Food vegetable = vegetablefactory.create();
                meat.print();
                vegetable.print();
                Console.ReadLine();
            }
        }
        public interface Food
        {
            void print();
        }
    
        public class Meat : Food
        {
            public void print()
            {
                Console.WriteLine("Meat");
            }
        }
    
        public class Vegetable : Food
        {
            public void print()
            {
                Console.WriteLine("vegetable");
            }
        }
    
        public interface Factory
        {
            Food create();
        }
    
        public class MeatFactory : Factory
        {
            public Food create()
            {
                return new Meat();
            }
        }
    
        public class VegetableFactory : Factory
        {
            public Food create()
            {
                return new Vegetable();
            }
        }
    }

    3.抽象工厂模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _3.抽象工厂模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                SuperFactory factory1 = new Factory1();
                Meat meat = factory1.createMeat();
                Vegetable vegetable = factory1.createVegetable();
                meat.print();
                Console.ReadLine();
            }
        }
    
        public interface Meat
        {
            void print();
        }
        public class Pork : Meat
        {
            public void print()
            {
                Console.WriteLine("Pork");
            }
        }
    
    
    
        public interface Vegetable
        {
            void print();
        }
        public class Carrot : Vegetable
        {
            public void print()
            {
                Console.WriteLine("Carrot");
            }
        }
        public interface SuperFactory
        {
            Meat createMeat();
            Vegetable createVegetable();
        }
    
        public class Factory1 : SuperFactory
        {
            public Meat createMeat()
            {
                return new Pork();
            }
            public Vegetable createVegetable()
            {
                return new Carrot();
            }
    
        }
    }
  • 相关阅读:
    十大Intellij IDEA快捷键
    多媒体播放API 生命周期束&简介
    Bitmap
    Activity
    Android中的Handler总结
    Bitmap2
    smartimageview和多线程
    Service
    微软面试题 博弈论 经典案例 (参考答案)
    ANR和消息机制
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/12836652.html
Copyright © 2011-2022 走看看