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

《大话设计模式》中的第一章是一个用简单工厂模式构建的简易计算器的例子,在书中的P10-P11页中有个工厂类OperaationFactory用来构造各个运算类的实例,但这里有个问题:如果需要增加新的运算类,除了要修改界面的代码,还要在OperaationFactoryswitch中增加新的语句!以下用反射很好的解决了这个问题~~~

 

///以下类编译为 .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;

        }

 

 

    }

}

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

**************************

 

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

OperationAdd.cs

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

using System;

using System.Collections.Generic;

using System.Text;

 

namespace oper

{

    /// <summary>

    /// 加法类

    /// </summary>

    class OperationAdd : Operation

    {

        public override double getResult()

        {

            double result = 0;

            result = NumberA + NumberB;

            return result;

        }

    }

}

 

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

**************************

 

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

OperationSub.cs

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

using System;

using System.Collections.Generic;

using System.Text;

 

namespace oper

{

    /// <summary>

    /// 减法类

    /// </summary>

    class OperationSub : Operation

    {

        public override double getResult()

        {

            double result = 0;

            result = NumberA - NumberB;

            return result;

        }

    }

}

 

 

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

**************************

 

 

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

OperationMul.cs

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

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace oper

{

    /// <summary>

    /// 乘法类

    /// </summary>

    class OperationMul : Operation

    {

        public override double getResult()

        {

            double result = 0;

            result = NumberA * NumberB;

            return result;

        }

    }

}

 

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

**************************

 

 

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

OperationDiv.cs

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

using System;

using System.Collections.Generic;

using System.Text;

 

namespace oper

{

    /// <summary>

    /// 除法类

    /// </summary>

    class OperationDiv : Operation

    {

        public override double getResult()

        {

            double result = 0;

            if (NumberB == 0)

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

            result = NumberA / NumberB;

            return result;

        }

    }

}

 

 

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

**************************

 

///以下类编译为 .dll///结束/

 

 

 

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

OperationFactory.cs

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

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

 

namespace oper

{

    /// <summary>

    /// 运算类工厂

    /// </summary>

    public class OperationFactory

    {

        public static Operation createOperate(string dllpath)

        {

            //operate = "oper." + operate;

            Operation oper;

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

            //oper = (Operation)obj;

            Assembly ass = Assembly.LoadFrom(dllpath+".dll");

            dllpath="oper." + dllpath;

            Type type=ass.GetType(dllpath);//利用类型的命名空间和名称获得类型

            Object obj = Activator.CreateInstance(type);//利用指定的参数实例话类型

            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)
        {
            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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值