欢迎访问我的博客 https://javascript.shop

设计模式学习草稿:简单工厂,工厂,抽象工厂

原文发布时间为:2008-11-27 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Collections.Generic;
using System.Text;

namespace reviewBase
{
    class Class1//简单工厂
    {
        static void Main()
        {
            string expression1 = "2+3";
            ICalculate result1 = SimpleFactory.getCaculate(expression1);
            Console.WriteLine(expression1 + "=" + result1.Excute(expression1));
            string expression2 = "2-3";
            ICalculate result2 = SimpleFactory.getCaculate(expression2);
            Console.WriteLine(expression2 + "=" + result2.Excute(expression2));
            Console.ReadLine();
        }
    }

    public interface ICalculate
    {
        int Excute(string Expressions);
    }
    public class Add : ICalculate
    {
        public int Excute(string Expression)
        {
            string[] num = Expression.Split('+');
            int a = int.Parse(num[0]);
            int b = int.Parse(num[1]);
            return a + b;
        }
    }
    public class Sub : ICalculate
    {
        public int Excute(string Expression)
        {
            string[] num = Expression.Split('-');
            int a = int.Parse(num[0]);
            int b = int.Parse(num[1]);
            return a - b;
        }
    }

    public class SimpleFactory
    {
        public static ICalculate getCaculate(string Expression)
        {
            if (Expression.Contains("+"))
                return new Add();
            else
                return new Sub();
        }
    }
}

---------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace reviewBase
{
    class Class2//工厂
    {
        static void Main()
        {
            string expression1 = "2+3";
            IFactory test1 = new Factory1();
            ICalculate result1=test1.getCaculate();
            Console.WriteLine(expression1 + "=" + result1.Excute(expression1));

            string expression2="2-3";
            IFactory test2 = new Factory2();
            ICalculate result2 = test2.getCaculate();
            Console.WriteLine(expression2 + "=" + result2.Excute(expression2));

            Console.ReadLine();
        }
    }

    public interface ICalculate
    {
        int Excute(string Expressions);
    }
    public class Add : ICalculate
    {
        public int Excute(string Expression)
        {
            string[] num = Expression.Split('+');
            int a = int.Parse(num[0]);
            int b = int.Parse(num[1]);
            return a + b;
        }
    }
    public class Sub : ICalculate
    {
        public int Excute(string Expression)
        {
            string[] num = Expression.Split('-');
            int a = int.Parse(num[0]);
            int b = int.Parse(num[1]);
            return a - b;
        }
    }
    public interface IFactory
    {
        ICalculate getCaculate();
    }
    public class Factory1:IFactory
    {
        public ICalculate getCaculate()
        {
                return new Add();
        }
    }
    public class Factory2 : IFactory
    {
        public ICalculate getCaculate()
        {
            return new Sub();
        }
    }
}
---------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace reviewBase
{
    class Class4//抽象工厂
    {
        static void Main()
        {
            Farm fruit1, vagetabel1,fruit2,vagetabel2;
            fruit1 = new AFarm();
            vagetabel1 = new AFarm();
            fruit1.factoryFruitFarm().PrintName();
            vagetabel1.factoryVagFarm().PrintName();
            fruit2 = new BFarm();
            vagetabel2 = new BFarm();
            fruit2.factoryFruitFarm().PrintName();
            vagetabel2.factoryVagFarm().PrintName();
            Console.ReadLine();
        }
    }

    public abstract class Farm
    {
        public abstract FruitFarm factoryFruitFarm();
        public abstract VagetabelFarm factoryVagFarm();
    }

    public class AFarm : Farm
    {
        public override FruitFarm factoryFruitFarm()
        {
            return new Apple();
        }
        public override VagetabelFarm factoryVagFarm()
        {
            return new BaiCai();
        }
    }

    public class BFarm : Farm
    {
        public override FruitFarm factoryFruitFarm()
        {
            return new Banana();
        }
        public override VagetabelFarm factoryVagFarm()
        {
            return new QingCai();
        }
    }

    public abstract class FruitFarm
    {
        public abstract void PrintName();
    }

    public class Apple : FruitFarm
    {
        public override void PrintName()
        {
            Console.WriteLine("apple");
        }
    }
    public class Banana : FruitFarm
    {
        public override void PrintName()
        {
            Console.WriteLine("banana");
        }
    }
    public abstract class VagetabelFarm
    {
        public abstract void PrintName();
    }
    public class BaiCai:VagetabelFarm
    {
        public override void PrintName()
        {
            Console.WriteLine("白菜");
        }
    }

    public class QingCai : VagetabelFarm
    {
        public override void PrintName()
        {
            Console.WriteLine("青菜");
        }
    }

}

posted @ 2017-07-11 00:37  孑孓子  阅读(104)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop