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();
            }
    
        }
    }
  • 相关阅读:
    安卓开发_浅谈TimePicker(时间选择器)
    eclipse显示代码行数
    Java数据解析---JSON
    Java数据解析---PULL
    Java数据解析---SAX
    统计机器学习(目录)
    FP Tree算法原理总结
    梯度下降(Gradient Descent)小结
    用scikit-learn和pandas学习线性回归
    用scikit-learn学习BIRCH聚类
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/12836652.html
Copyright © 2011-2022 走看看