码迷,mamicode.com
首页 > 其他好文 > 详细

六、创建型模式之简单工厂模式-----《大话设计模式》

时间:2015-09-17 23:10:49      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

简单工厂模式:

    需要增加运算形式时只需要增加运算子类,同时修改简单工厂中的switch增加分支即可,这样客户端调用代码基本不动。

技术分享

    // 运算类
    public class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;
        
        // 数字A属性     
        public double NumberA
        {
            get
            {
                return _numberA;
            }
            set
            {
                _numberA = value;
            }
        }

        // 数字B属性
        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 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;
                    }
            }
            return oper;
        }
    }
//控制台调用
static void Main(string[] args)
        {
            try
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                Console.Write("请选择运算符号(+、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                string strResult = "";

                Operation oper;                                         //声明运算类
                oper = OperationFactory.createOperate(strOperate);      //根据运算符调用工厂类初始化相应运算子类
                oper.NumberA = Convert.ToDouble(strNumberA);
                oper.NumberB = Convert.ToDouble(strNumberB);
                strResult = oper.GetResult().ToString();

                Console.WriteLine("结果是:" + strResult);

                Console.ReadLine();


            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错:" + ex.Message);
            }
        }

六、创建型模式之简单工厂模式-----《大话设计模式》

标签:

原文地址:http://www.cnblogs.com/shanymen/p/4817796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!