最佳化後《簡單原廠模式》構造的計算機代碼—C#“反射”技術(dll)

來源:互聯網
上載者:User

《大話設計模式》中的第一章是一個用簡單原廠模式構建的簡易計算機的例子,在書中的P10-P11頁中有個工廠類OperaationFactory用來構造各個運算類的執行個體,但這裡有個問題:如果需要增加新的運算類,除了要修改介面的代碼,還要在OperaationFactory的switch中增加新的語句!以下用反射很好的解決了這個問題~~~

 

///////////以下類編譯為 .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);            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.