三种工厂模式的比较

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();
        }

    }
}

 

posted @ 2020-05-06 15:27  当年小清新  阅读(345)  评论(0编辑  收藏  举报