設計模式的第一個例子之後,我再次體會到了做一個軟件,不僅僅是為了功能的實現,更多的是“為人民服務”!
不用面向對象的方法去寫,要實現一個簡單的只有“+ - * /”計算器,那是相當簡單的,只需獲取用戶輸入的數字和運算符,然後“A運算B”得出結果顯示出來就行了。如下代碼所示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) {
//輸入要運算的數字和運算符 Console.WriteLine("請輸入數字A"); double A =Convert .ToDouble ( Console.ReadLine()); Console.WriteLine("請輸入運算符"); string operate = Console.ReadLine(); Console.WriteLine("請輸入數字B"); double B = Convert.ToDouble(Console.ReadLine());
//將結果存儲到變量D裡面 double D=0;
//用if條件語句判斷輸入的符號而進行相應的運算並輸出 if (operate =="+") { D = A + B; Console.WriteLine("A+B=" + D); } if (operate =="-") { D = A - B; Console.WriteLine("A-B=" +D); } if (operate =="*") { D = A * B; Console.WriteLine("A*B=" +D); } if (operate =="/") { D = A / B; Console.WriteLine("A/B=" + D); } } } }
如果用戶的需求發生了變化了呢?比如想再加個開根號的運算,然後就加個if語句,在輸入項裡也要改動,而且當除數為零時,還會發生錯誤,直接導致程序死掉,這些變化的產生,對於修改程序是一件非常麻煩的事,小程序還好,大程序的話就特別容易導致錯誤的發生,而且不易於修改,很可能修改一個地方,卻導致了另一個地方出現問題,這種整個程序的程序塊與程序塊之間有著組合的關系,非常不利於維護和擴展,所以,有了面向對象的思想。
應該把整個程序抽象出一些類來,每個實現一部分功能,就如用於顯示的就抽象一個顯示類,運算的為運算類,還有運算符類等等,當程序需要擴展增加某項功能時候,我們只需新增一個類就行,跟別的沒關系,所以非常易於維護和擴展。修改後的代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工廠模式_計算器2 { class Program { static void Main(string[] args) { try //如果遇到錯誤,進行提示 {
//用於輸入 Console.WriteLine("請輸入數字A"); double _numA =Convert.ToDouble( Console.ReadLine()); Console.WriteLine("請輸入運算符(+、-、*、/)"); string _operate = Convert.ToString ( Console.ReadLine()); Console.WriteLine("請輸入數字B"); double _numB = Convert.ToDouble(Console.ReadLine()); double _result; //保存結果的變量 Operation oper; oper = operationFactory.createOperate("+"); oper.numberA = _numA; //給類屬性賦值 oper.numberB = _numB; operationFactory.createOperate(_operate); _result = oper.GetResult(); Console.WriteLine(_result); //輸出結果 } catch { Console.WriteLine("輸入有誤!"); } } } public class Operation //運算父類 { private double _numberA = 0; private double _numberB = 0; public double numberA { get { return _numberA; } set { _numberA = value; } } public double numberB { get { return _numberB; } set { _numberB = value; } } public virtual double GetResult() { double result = 0; return result; } } public class operationAdd:Operation //繼承運算父類的加法運算 { public override double GetResult() { double result = 0; result = numberA + numberB; return result; } } public class operationSub:Operation //繼承運算父類的減法運算 { public override double GetResult() { double result = 0; result = numberA - numberB; return result; } } public class operationMul:Operation //繼承運算父類的乘法運算 { public override double GetResult () { double result = 0; result = numberA * numberB; return result; } } public class operationDiv :Operation //繼承運算父類的除法運算 { public override double GetResult() { double result; result = numberA / numberB; return result; } } public 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; } } }