1. 程式人生 > >C#工廠模式程式碼例項

C#工廠模式程式碼例項

此處示例為一個簡易計算器工廠模式的實現.
建立類庫,名為CalcLib,我把計算功能全部放在這個類庫中.
首先,建立一個抽象的計算器演算法父類,如下:

    /// <summary>
    /// 計算器演算法父類
    /// </summary>
    public abstract class Calculator
    {
        public Calculator()
        {
        }

        //給子類的引數
        public Calculator(double n1, double n2)
        {
            this
.Number1 = n1; this.Number2 = n2; } public double Number1 { get; set; } public double Number2 { get; set; } /// <summary> /// 抽象計算方法 /// </summary> /// <returns>result</returns> public abstract double Compute(); }

父類建立完畢後,我們在子類來實現具體的加減乘除
加法類:

    /// <summary>
    /// 加法
    /// </summary>
    public class CalAdd:Calculator
    {
        public CalAdd(double n1, double n2)
            :base(n1,n2)
        {

        }

        public override double Compute()
        {
            return Number1 + Number2;
        }
    }

減法類:

    /// <summary>
    /// 減法
    /// </summary>
    public class CalSub:Calculator
    {
        public CalSub(double n1, double n2)
            :base(n1,n2)
        {

        }

        public override double Compute()
        {
            return Number1 - Number2;
        }
    }

乘法類:

    /// <summary>
    /// 乘法
    /// </summary>
    public class CalMul:Calculator
    {
        public CalMul(double n1, double n2)
            :base(n1,n2)
        {

        }

        public override double Compute()
        {
            return Number1 * Number2;
        }
    }

除法類:

    /// <summary>
    /// 除法
    /// </summary>
    public class CalDiv : Calculator
    {
        public CalDiv(double n1, double n2)
            : base(n1, n2)
        {

        }

        public override double Compute()
        {
            return Number1 / Number2;
        }
    }

OK,需要的計算方法都建立完畢了,現在,我們來建立主程式,這裡呢我建立一個控制檯程式,名為MyCalculator,然後呢在Program類中新增一個靜態的計算工廠方法。

        /// <summary>
        /// 計算工廠
        /// </summary>
        /// <param name="n1">運算元1</param>
        /// <param name="op">符號</param>
        /// <param name="n2">運算元2</param>
        /// <returns>計算結果</returns>
        static Calculator Cal(double n1, string op, double n2)
        {
            Calculator cal = null;
            switch (op)
            {
                case "+":
                    cal = new CalAdd(n1, n2);
                    break;
                case "-":
                    cal = new CalSub(n1, n2);
                    break;
                case "*":
                    cal = new CalMul(n1, n2);
                    break;
                case "/":
                    cal = new CalDiv(n1, n2);
                    break;
                default:
                    break;
            }
            return cal;
        }

最後呢我在Main方法中添加了以下程式碼:

static void Main(string[] args)
        {
            Console.Title = "簡易計算器";
            Console.WindowWidth = 37;
            Console.WindowHeight = 10;
            Console.ForegroundColor = ConsoleColor.Green;
            while (true)
            {
                try
                {
                    Console.Clear();
                    Console.WriteLine("請輸入運算元1:");
                    double n1 = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("請輸入操作符:");
                    string op = Console.ReadLine();
                    Console.WriteLine("請輸入運算元2:");
                    double n2 = Convert.ToDouble(Console.ReadLine());
                    Calculator cal = Cal(n1, op, n2);
                    if (cal != null)
                        Console.WriteLine("計算結果為:{0}", cal.Compute());
                    Console.WriteLine("再次按回車繼續計算...");
                    Console.ReadLine();
                }
                catch { break; }
            }
        }

OK,一個簡單的工廠模式實現就完成了,用工廠模式首先呢要有一個公用的抽象意義,然後再來使用,新功能無需更改任何程式碼,只需要新增一個類,基礎父類,然後實現你要得功能就可以了,有點類似於瀏覽器和外掛。