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


    原文发布时间为: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("青菜");
            }
        }

    }

  • 相关阅读:
    AYIT609暑假集训第一周下训练题题解
    AYIT609暑假集训第一周上训练题题解
    IELTS
    CodeForces451B
    CodeForces1352C
    CodeForces474B
    提供程序-java根据纬度 经度值获取用户的地理位置
    一款java写的将汉字转为拼音的工具类(转)
    MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping(转)
    MyBatis学习3---使用SqlBuilder生成SQL语句(转)
  • 原文地址:https://www.cnblogs.com/handboy/p/7148496.html
Copyright © 2020-2023  润新知