大話設計模式(四 業務的封裝)_大話設計模式

來源:互聯網
上載者:User
(續上篇) 

        小菜:“你的意思是分一個類出來。 哦,對的,讓計算和顯示分開。”
        大鳥:“準確的說,就是讓商務邏輯與介面邏輯分開,讓它們之間的耦合度下降。只有分離開,才容易達到容易維護或擴充。”
        小菜:“讓我來試試看。”

class Program    {        static void Main(string[] args)        {            try            {                Console.Write("請輸入數字A:");                string strNumberA = Console.ReadLine();                Console.Write("請選擇運算子號(+、-、*、/):");                string strOperate = Console.ReadLine();                Console.Write("請輸入數字B:");                string strNumberB = Console.ReadLine();                string strResult = "";                strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA),Convert.ToDouble(strNumberB),strOperate));                 Console.WriteLine("結果是:" + strResult);                Console.ReadLine();            }            catch (Exception ex)            {                Console.WriteLine("您的輸入有錯:" + ex.Message);            }        }    }    public class Operation    {        public static double GetResult(double numberA,double numberB,string operate)        {            double result = 0d;            switch (operate)            {                case "+":                    result = numberA + numberB;                    break;                case "-":                    result = numberA - numberB;                    break;                case "*":                    result = numberA * numberB;                    break;                case "/":                    result = numberA / numberB;                    break;            }            return result;        }    }


   小菜:“鳥哥,我寫好了,你看看。”

        大鳥:“哈,孺鳥可教也,:),寫得不錯,這樣就完全把業務和介面分離了。”
        小菜心中暗罵:“你才是鳥呢。”    口中說道:“如果你現在要我寫一個Windows應用程式的計算機,我就可以複用這個運算類(Operation)了。”
        大鳥:“不單是Windows程式,Web版程式需要運算可以用它,PDA,手機等需要移動系統的軟體需要運算也可以用它呀。”
        小菜:“哈,物件導向不過如此。下會寫類似代碼不怕了。”
        大鳥:“別急,僅此而已,實在談不上完全物件導向,你只用了物件導向三大特性的一個,還兩個沒用呢。”
        小菜:“物件導向三大特性不就是封裝、繼承和多態嗎,這裡我用到的應該是封裝。這還不夠嗎。…………我實在看不出,這麼小的程式如何用到繼承。至於多態,其它我一直也不太瞭解它到底有什麼好處,如何使用它。”
        大鳥:“慢慢來,有的東西好學了,你好好想想吧,我要去“魔獸”了,改時聊。”

(待續)

出處:http://www.cnblogs.com/cj723/archive/2006/08/31/491022.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.