优化后《简单工厂模式》构造的计算器代码—C#“反射”技术

修改后的计算器代码,这里有个这样问题:如果需要增加新的运算类,除了要修改界面的代码,还要在OperaationFactoryswitch中增加新的语句!运用C#反射技术,可以很好的解决问题,以下代码不是运用dll,举个例子

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

Operation.cs

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

using System;

using System.Collections.Generic;

using System.Text;

 

namespace oper

{

    /// <summary>

    /// 运算类

    /// </summary>

    public class Operation

    {

        private double _numberA = 0;

        private double _numberB = 0;

 

        /// <summary>

        /// 数字A

        /// </summary>

        public double NumberA

        {

            get

            {

                return _numberA;

            }

            set

            {

                _numberA = value;

            }

        }

 

        /// <summary>

        /// 数字B

        /// </summary>

        public double NumberB

        {

            get

            {

                return _numberB;

            }

            set

            {

                _numberB = value;

            }

        }

 

        /// <summary>

        /// 得到运算结果

        /// </summary>

        /// <returns></returns>

        public virtual double getResult()

        {

            double result = 0; 

            return result;

        }

 

 

    }

 

    /// <summary>

    /// 加法类

    /// </summary>

    class OperationAdd : Operation

    {

        public override double getResult()

        {

            double result = 0; 

            result = NumberA + NumberB;

            return result;

        }

    }

 

    /// <summary>

    /// 减法类

    /// </summary>

    class OperationSub : Operation

    {

       public override double getResult()

        {

            double result = 0;

            result = NumberA - NumberB;

            return result;

        }

    }

 

    /// <summary>

    /// 乘法类

    /// </summary>

    class OperationMul : Operation

    {

        public override double getResult()

        {

            double result = 0;

            result = NumberA * NumberB;

            return result;

        }

    }

 

    /// <summary>

    /// 除法类

    /// </summary>

    class OperationDiv : Operation

    {

        public override double getResult()

        {

            double result = 0;

            if (NumberB==0)

                throw new Exception("除数不能为0。");

            result = NumberA / NumberB;

            return result;

        }

    }

 

    /// <summary>

    /// 运算类工厂

    /// </summary>

    class OperationFactory

    {

        public static Operation createOperate(string operate)

        {

            operate = "oper." + operate;

            Operation oper;

            Object obj = Activator.CreateInstance(Type.GetType(operate));

            oper = (Operation)obj;

            return oper;

        }

    }

 

}

-----------------------------------------
Program.cs
-----------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace oper
{
    class Program
    {
        static void Main(string[] args)
        {
            //OperationAdd OperationSub OperationMul OperationDiv
            Operation oper;
            while (true)
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                ///
                ///选择运算符可以从配置文件XML读取
                ///
                Console.Write("请选择运算符号:/nOperationAdd代表加(+)/nOperationSub代表减(-)/nOperationMul代表乘(*)/nOperationDiv代表除(/)/n");
                string strOperate = Console.ReadLine();
                oper = OperationFactory.createOperate(strOperate);
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                oper.NumberA = Convert.ToDouble(strNumberA);
                oper.NumberB = Convert.ToDouble(strNumberB);
                double result = oper.getResult();
                Console.WriteLine("result : " + result);
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值