代码改变世界

大话设计模式之简单工场模式

2014-04-20 19:38  微尘_无名  阅读(176)  评论(0编辑  收藏  举报

 此文中以活字印刷的故事来类比面向对象程序设计,可谓见解独到:

  1、只需修改所改之字,谓之可维护;

  2、此间所刻之字今后可复用,谓之可复用;

  3、要加新的内容,可在原有的内容上加,谓之可扩展;

此文以计算器的实现过程的逐步优化,逐步实现了分装、继承、多态,实现了业务逻辑和界面逻辑的分离,使得业务逻辑不仅可以用在windows上,在手机、PDA上面也可以用,真正实现了软件工程的可维护、可复用、可扩展。个人觉得简单工场模式的应用场景是:可将用于switch的操作的每个操作抽象为一个类,共同继承一个基类,再定义一个工厂类,在客户端调用工厂类即可得到具体的操作了。

整个代码整理如下:

业务逻辑:

public class Operation
    {
        private double _numberA = 0d;
        private double _numberB = 0d;

        public double numberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }

        public double numberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }

        public virtual double getResult()
        {
            double result = 0d;
            return result;
        }
    }

class OperationAdd:Operation
    {
        public override double getResult()
        {
            double result = 0d;
            result = numberA + numberB;
            return result;
        }
    }

class OperationSub:Operation
    {
        public override double getResult()
        {
            double result = 0d;
            result = numberA - numberB;
            return result;
        }
    }

class OperationMul:Operation
    {
        public override double getResult()
        {
            double result = 0d;
            result = numberA * numberB;
            return result;
        }
    }

class OperationDiv:Operation
    {
        public override double getResult()
        {
            if (0 == numberB)
                throw new Exception("The divisor can't be zoro");
            double result = 0d;
            result = numberA / numberB;
            return result;
        }
    }

public class OperationFactory
    {
        public static Operation createOperation(string operate)
        {
            Operation op = null;
            switch (operate)
            {
                case "+":
                    op = new OperationAdd();
                    break;
                case "-":
                    op = new OperationSub();
                    break;
                case "*":
                    op = new OperationMul();
                    break;
                case "/":
                    op = new OperationDiv();
                    break;
            }
            return op;
        }
    }
View Code

界面逻辑:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Please enter the number A:");
                string strNumberA = Console.ReadLine();
                Console.Write("Please enter the opeator(+,-,*,/)");
                string strOperator = Console.ReadLine();
                Console.Write("Please enter the number B:");
                string strNumberB = Console.ReadLine();

                Operation oper = OperationFactory.createOperation(strOperator);
                oper.numberA=Convert.ToDouble(strNumberA);
                oper.numberB=Convert.ToDouble(strNumberB);
                string result = Convert.ToString(oper.getResult());
                Console.WriteLine("The result is: " + result);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error ocurrs:" + ex.Message);
            }
            Console.ReadLine();
        }
    }
View Code