• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 0,  comments - 5,  trackbacks - 0
            簡單工廠模式
            1.1 面試受挫
            #include <iostream>

            using namespace std;

            int main(void)
            {
                cout 
            << "請輸入數字" << endl;
                
            int A = 0;
                cin 
            >> A;

                cout 
            << "請輸入運算符號(+、-、*、/)" << endl;
                
            char B = '+';
                cin 
            >> B;
                
                cout 
            << "請輸入數字" << endl;
                
            int C = 0;
                cin 
            >> C;

                
            int D = 0;

                
            if ( B == '+' )
                    D 
            = A + C;
                
            if ( B == '-' )
                    D 
            = A - C;
                
            if ( B == '*' )
                    D 
            = A * C;
                
            if ( B == '/' )
                    D 
            = A / C;

                cout 
            << "結果是" << D << endl;
                
            return 0;
            }


            1.2 初學者的毛病
            (
            1) A,B,C,D的變量命名不規范
            (
            2) 判斷分支,意味著每個條件都要判斷,計算機額外做了三次判斷操作
            (
            3) 沒有判斷除0的條件
            (
            4) 沒有考慮到用戶輸入出錯的問題

            1.3 規范代碼
            #include <iostream>

            using namespace std;

            // exception constant
            #define DIVID_ZERO_ERROR 0

            // function return constant
            #define OK 0
            #define ERROR -1


            //from input stream read a integer
            int ReadInt ( char *tipMessage = "Input Error,you must enter an integer,please again:")
            {
                
            int readResult = 0;
                cin 
            >> readResult;
                
            while ( cin.fail() )
                {
                    cout 
            << tipMessage << endl; //show the user a tip message

                    cin.clear(); 
            //reset the fail flag to zero
                    cin.sync(); //clear the data of the input stream

                    cin 
            >> readResult;
                }
                
            return readResult;
            }

            int main(void)
            {
                
            try
                {
                    cout 
            << "Please enter an integer:" << endl;
                    
            int numberA = ReadInt ();
                    
                    cout 
            << "Please enter an operator(+、-、*、/)" << endl;
                    
            char arithOperator = '+';
                    cin 
            >> arithOperator;
                    
                    cout 
            << "Please enter an integer:" << endl;
                    
            int numberB = ReadInt ();
                    
                    
            int result = 0;
                    
                    
            switch ( arithOperator )
                    {
                    
            case '+':
                        result 
            = numberA + numberB;
                        
            break;

                    
            case '-':
                        result 
            = numberA - numberB;
                        
            break;

                    
            case '*':
                        result 
            = numberA * numberB;
                        
            break;

                    
            case '/':
                        
            if ( numberB == 0 )
                        {
                            
            throw (DIVID_ZERO_ERROR); 
                        }
                        
            else
                        {
                            result 
            = numberA / numberB;
                        }
                        
            break;

                    
            default:
                        
            break;
                    }
                    
                    
            //out put the result
                    cout << "The result is " << result << endl;
                }
                
            catch ( int errorId)
                {
                    
            switch (errorId)
                    {
                        
            case DIVID_ZERO_ERROR:
                            cout 
            << "Dividing a zero is an error!" << endl;
                            
            break;

                        
            default:
                            cout 
            << "unknown error id!";
                    }
                    
            return ERROR;
                }
                
            return OK;
            }


            1.4 面向對象編程
            (
            1)可維護
            (
            2)可復用
            (
            3)可擴展
            (
            4)靈活性好

            1.6
            通過封裝、繼承、多態把程序的耦合度降低,用設計模式使得程序更加的靈活,容易修改,并且易于復用。
            1.7
            小菜: 復制還是復用,這是個問題。
            大鳥: 問題你個頭,當然是復用啦!
            1.8
            業務邏輯應該與界面邏輯分開,讓它們之間的耦合度降低。
            分開才容易維護或者擴展。
            #include <iostream>

            using namespace std;

            #define DIVIED_ZERO_ERROR 0
            #define NO_DEFINED_OPERATOR 1

            #define OK 0
            #define ERROR -1

            class ArithOperation
            {
            public:
                
            static double GetResult (double numberA, double numberB, char arithOperator)
                {
                    
            double result = 0;
                    
            switch ( arithOperator)
                    {
                    
            case '+':
                        result 
            = numberA + numberB;
                        
            break;
                        
                    
            case '-':
                        result 
            = numberA - numberB;
                        
            break;
                        
                    
            case '*':
                        result 
            = numberA * numberB;
                        
            break;
                        
                    
            case '/':
                        
            if (numberB == 0)
                            
            throw (DIVIED_ZERO_ERROR);
                        result 
            = numberA / numberB;
                        
            break;
                    
            default:
                        
            throw ( NO_DEFINED_OPERATOR );
                        
            break;
                    }
                    
                    
            return result;
                }
            };


            double ReadDouble ( char * tipMessage = "Input Error!Please enter an double number again:")
            {
                
            double result = 0.0;
                cin 
            >> result;
                
            while (cin.fail())
                {
                    cout 
            << tipMessage << endl;
                    
                    cin.clear();
                    cin.sync();
                    
                    cin 
            >> result;
                }
                
                
            return result;
            }

            int main (void)
            {
                
            try
                {
                    cout 
            << "Please Enter an dobule number:" << endl;
                    
            double numberA = ReadDouble ();
                    
                    cout 
            << "Please Enter an arithmetic operator:" << endl;
                    
            char arithOperator;
                    cin 
            >> arithOperator;
                    
                    cout 
            << "Please Enter an double number:" << endl;
                    
            double numberB = ReadDouble ();
                    
                    
            double result = 0;
                    result 
            = ArithOperation::GetResult ( numberA, numberB, arithOperator );
                    
                    cout 
            << "The result is" << result << endl;
                }
                
            catch ( int errorId )
                {
                    
            switch (errorId)
                    {
                    
            case DIVIED_ZERO_ERROR:
                        cout 
            << "Dividing zero is error." << endl;
                        
            break;

                    
            case NO_DEFINED_OPERATOR:
                        cout 
            << "The opeartor is not defined." << endl;
                        
            break;
                        
                    
            default:
                        cout 
            << "unknow error!" << endl;
                        
            break;
                    }
                    
            return ERROR;
                }
                
            return OK;
            }


            1.9 緊耦合和松耦合
            #include <iostream>

            using namespace std;

            #define DIVIED_ZERO_ERROR 0
            #define OPERATION_TYPE_ERROR 1

            #define OK 0
            #define ERROR 1

            class arithOperator
            {
            protected:
                
            double numberA;
                
            double numberB;
            public:
                arithOperator()
                {
                    numberA 
            = 0;
                    numberB 
            = 0;
                }

                
            void SetNumberA (double number)
                {
                    numberA 
            = number;
                }
                
            double GetNumberA ()
                {
                    
            return numberA;
                }
                
            void SetNumberB (double number)
                {
                    numberB 
            = number;
                }
                
            double GetNumberB ()
                {
                    
            return numberB;
                }

                
            virtual double GetResult ()
                {
                    
            double result = 0;
                    
            return result;
                }
            };

            class OpeartionAdd : public arithOperator
            {
                
            virtual double GetResult ()
                {
                    
            double result = 0;
                    result 
            = numberA + numberB;
                    
            return result;
                }
            };

            class OperationSub : public arithOperator
            {
                
            virtual double GetResult ()
                {
                    
            double result = 0;
                    result 
            = numberA - numberB;
                    
            return result;
                }
            };

            class OperationMul : public arithOperator
            {
                
            virtual double GetResult ()
                {
                    
            double result = 0;
                    result 
            = numberA * numberB;
                    
            return result;
                }
            };

            class OperationDiv : public arithOperator
            {
                
            virtual double GetResult ()
                {
                    
            if (numberB == 0)
                    {
                        
            throw (DIVIED_ZERO_ERROR);
                    }

                    
            double result = 0;
                    result 
            = numberA / numberB;

                    
            return result;
                }
            };


            double ReadDouble ( char * tipMessage = "Input error!Please enter an double number again:\n")
            {
                
            double result = 0;

                cin 
            >> result;
                
            while (cin.fail())
                {
                    cout 
            << tipMessage << endl;
                    cin.clear();
                    cin.sync();
                    cin 
            >> result;
                }
                
            return result;
            }

            class OperationFactory
            {
            public:
                
            static arithOperator* createOperator( char operType)
                {
                    arithOperator 
            *operPointer = NULL;
                    
            switch (operType)
                    {
                    
            case '+':
                        operPointer 
            = new OpeartionAdd();
                        
            break;

                    
            case '-':
                        operPointer 
            = new OperationSub();
                        
            break;

                    
            case '*':
                        operPointer 
            = new OperationMul();
                        
            break;

                    
            case '/':
                        operPointer 
            = new OperationDiv();
                        
            break;

                    
            default:
                        
            throw OPERATION_TYPE_ERROR;
                        
            break;
                    }
                    
            return operPointer;
                }
            };

            int main (void)
            {
                
            try
                {
                
                    cout 
            << "Please enter an double number:" << endl;
                    
            double numberA;
                    numberA 
            = ReadDouble();
                    
                    cout 
            << "Please enter an operator:" << endl;
                    
            char userOperator;
                    cin 
            >> userOperator;
                    
                    cout 
            << "Please enter an double number:" << endl;
                    
            double numberB;
                    numberB 
            = ReadDouble();

                    arithOperator 
            *operPointer;
                    operPointer 
            = OperationFactory::createOperator (userOperator);
                    operPointer
            ->SetNumberA(numberA);
                    operPointer
            ->SetNumberB(numberB);
                    
                    
            double result = 0;
                    result 
            = operPointer->GetResult();

                    cout 
            << "The result is " << result << endl;
                    
                    delete operPointer;

                    
            return OK;

                }
                
            catch ( int errorId )
                {
                    
            switch (errorId)
                    {
                    
            case DIVIED_ZERO_ERROR:
                        cout 
            << "Diving zero is an error!" << endl;
                        
            break;

                    
            case OPERATION_TYPE_ERROR:
                        cout 
            << "Operator type is error!" << endl;
                        
            break;

                    
            default:
                        cout 
            << "Unknown error!" << endl;
                        
            break;
                    }

                    
            return ERROR;
                }
                
            }


            總結:工廠只負責生產由用戶指定的產品,這樣的設計好處是每種產品獨立,如果程序需要修改其中某一個產品,那么他不需要改動其它產品的代碼,也不需要改動工廠的代碼,當然,界面代碼也不需要改動,改動的只是產品的內在。
            如果要添加一種新的產品,則需添加新的類并修改工廠類。
            注意:產品必須是同一種功能,也就是同樣的方法名稱。

            C
            ++步驟:
            1.定義抽象類,并定義一個虛擬的方法
            2.從該抽象類派生出子類,實現同一個方法
            3.定義工廠,提供函數,根據用戶的輸入,生產出不同的對象,注意,這里用的是NEW,所以用
            4.利用這個對象進行操作
            5.刪除對象
            posted on 2010-09-15 16:19 saha 閱讀(179) 評論(0)  編輯 收藏 引用

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿

            文章分類

            文章檔案

            收藏夾

            搜索

            •  

            最新評論

            99久久精品免费看国产| 亚洲女久久久噜噜噜熟女| 国产激情久久久久影院小草 | 热久久最新网站获取| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 大香伊人久久精品一区二区| 99精品国产99久久久久久97 | 久久夜色精品国产噜噜亚洲AV| 亚洲AV无码久久寂寞少妇| 2021少妇久久久久久久久久| 久久高潮一级毛片免费| 亚洲中文字幕无码久久综合网| 国内精品久久久久久久97牛牛| 精品无码久久久久久久久久| 色综合久久综合中文综合网| 久久se精品一区二区影院| 亚洲va久久久噜噜噜久久男同| 久久精品中文字幕一区| 99久久精品国产高清一区二区| 合区精品久久久中文字幕一区| 99久久99这里只有免费的精品| 一本色综合久久| 精品久久久久久久中文字幕 | 伊色综合久久之综合久久| 久久精品国产半推半就| 久久国产精品77777| 偷窥少妇久久久久久久久| 久久精品中文字幕第23页| 91久久精品电影| 久久伊人精品青青草原高清| 久久国产热精品波多野结衣AV| 中文字幕久久精品无码| 国产精品久久久香蕉| 精品伊人久久久| 久久亚洲熟女cc98cm| 欧美亚洲国产精品久久| 色综合久久久久综合99| 一本大道久久东京热无码AV | 亚洲中文字幕无码久久综合网| 久久久久青草线蕉综合超碰| 久久天天躁夜夜躁狠狠 |