大话设计模式-简单工厂模式

面向对象编程:
活字印刷术:
第一:要改,只需要改要改之处--可维护;
第二:这些字并非用完这次就无用,完全可以在后来的印刷中重复使用--复用性;
第三:此诗若要加字,只需另加入即可--扩展性
第四:字的排列其实可能是竖排可能是横排,此时只需要将活字移动就可做到满足排列需求--灵活性

好处:通过封装、继承、多态把程序的耦合度降低;开始用设计模式使得程序更加的灵活,容易修改,并且易于复用。

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

namespace Operation

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

        public double NumberA
        {
            get
            {
                return _numberA;
            }
            set
            {
                _numberA = value;
            }
        }
        public double NumberB
        {
            get
            {
                return _numberB;
            }
            set
            {
                _numberB = value;
            }
        }

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

        class OperationAdd : Operation
        {
            public override double getResult()
            {
                double result = 0;
                result = NumberA + NumberB;
                return result;
            }
        }

        class OperationSub : Operation
        {
            public override double getResult()
            {
                double result = 0;
                result = NumberA - NumberB;
                return result;
            }
        }

        class OperationMul : Operation
        {
            public override double getResult()
            {
                double result = 0;
                result = NumberA * NumberB;
                return result;
            }
        }

        class OperationDiv : Operation
        {
            public override double getResult()
            {
                double result = 0;
                if (NumberB == 0)
                    throw new Exception("除数不能为0");
                result = NumberA / NumberB;
                return result;
            }
        }

        class OperationSqr : Operation
        {
            public override double getResult()
            {
                double result = 0;
                result = NumberB * NumberB;
                return result;
            }
        }

        class OperationSqart : Operation
        {
            public override double getResult()
            {

                double result = 0;
                if (NumberA <0)
                    throw new Exception ("负数不能开平方根");
                result =Math.Sqrt (NumberB);
                return result ;
            }
        }

        class OperationReverse : Operation
        {
            public override double getResult()
            {
                double result = 0;
                result = -NumberB;
                return result;
            }
        }

        class OperationFactory
        {
            public static Operation createOperate(string operate)
            {
                Operation oper = null;
                switch (operate)
                {
                    case "+":
                        {
                            oper =new OperationAdd ();
                            break ;
                        }
                    case "-":
                        {
                            oper = new OperationSub();
                            break;
                        }
                    case "*":
                        {
                            oper = new OperationMul();
                            break;
                        }
                    case "/":
                        {
                            oper = new OperationDiv();
                            break;
                        }
                    case "sqr":
                        {
                            oper = new OperationSqr();
                            break;
                        }
                    case "sqrt":
                        {
                            oper = new OperationSqart();
                            break;
                        }
                    case "+/-":
                        {
                            oper = new OperationReverse();
                            break;
                        }
                }
                return oper;
            }
        }
    }
}

你可能感兴趣的:(设计模式)