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();
            }
    
        }
    }
  • 相关阅读:
    grep
    virtualbox共享文件夹无法创建软链接的解决方法
    openH264的双向链表实现
    openH264构建过程
    Ninja构建系统入门
    ubuntu上安装meson & 如何使用meson编译C代码
    ln: failed to create symbolic link ‘libopenh264.so.6’: Operation not permitted
    RAII-资源获取即初始化
    可变参数实现原理-参数栈
    一个统计多文件单行字符串出现次数QT实现
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/12836652.html
Copyright © 2011-2022 走看看