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();
            }
    
        }
    }
  • 相关阅读:
    The differences between genetic distance values of Nei 1978 and Nei 1972
    【打油诗】无题
    EXCEL函数LookUp, VLOOKUP及HLOOKUP函数
    【菜鸟入门R语言】独立t检验
    【菜鸟入门R语言】利用R语言画箱线图
    【综述】植物防御假说——Out of the quagmire of plant defense hypotheses
    一个创造奇迹的上午——谈如何高效利用时间
    两篇波兰地区重金属污染地区土壤微生物多样性的研究
    【软件使用】利用image J测定叶面积
    利用Peakscanner软件对测序仪获取的分子标记数据进行初步识别
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/12836652.html
Copyright © 2011-2022 走看看