• <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
            簡(jiǎn)單工廠模式
            1.1 面試受挫
            #include <iostream>

            using namespace std;

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

                cout 
            << "請(qǐng)輸入運(yùn)算符號(hào)(+、-、*、/)" << endl;
                
            char B = '+';
                cin 
            >> B;
                
                cout 
            << "請(qǐng)輸入數(shù)字" << 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 
            << "結(jié)果是" << D << endl;
                
            return 0;
            }


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

            1.3 規(guī)范代碼
            #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 面向?qū)ο缶幊?br>(1)可維護(hù)
            (
            2)可復(fù)用
            (
            3)可擴(kuò)展
            (
            4)靈活性好

            1.6
            通過封裝、繼承、多態(tài)把程序的耦合度降低,用設(shè)計(jì)模式使得程序更加的靈活,容易修改,并且易于復(fù)用。
            1.7
            小菜: 復(fù)制還是復(fù)用,這是個(gè)問題。
            大鳥: 問題你個(gè)頭,當(dāng)然是復(fù)用啦!
            1.8
            業(yè)務(wù)邏輯應(yīng)該與界面邏輯分開,讓它們之間的耦合度降低。
            分開才容易維護(hù)或者擴(kuò)展。
            #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;
                }
                
            }


            總結(jié):工廠只負(fù)責(zé)生產(chǎn)由用戶指定的產(chǎn)品,這樣的設(shè)計(jì)好處是每種產(chǎn)品獨(dú)立,如果程序需要修改其中某一個(gè)產(chǎn)品,那么他不需要改動(dòng)其它產(chǎn)品的代碼,也不需要改動(dòng)工廠的代碼,當(dāng)然,界面代碼也不需要改動(dòng),改動(dòng)的只是產(chǎn)品的內(nèi)在。
            如果要添加一種新的產(chǎn)品,則需添加新的類并修改工廠類。
            注意:產(chǎn)品必須是同一種功能,也就是同樣的方法名稱。

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

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



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

            常用鏈接

            留言簿

            文章分類

            文章檔案

            收藏夾

            搜索

            •  

            最新評(píng)論

            蜜臀久久99精品久久久久久| 一级做a爰片久久毛片免费陪| 狠狠色婷婷久久一区二区三区 | 99久久精品免费看国产一区二区三区 | 国产精品久久久久久久人人看| 91精品国产色综久久| 精品国产综合区久久久久久| 伊人久久一区二区三区无码| 久久精品中文字幕大胸| 1000部精品久久久久久久久| 午夜精品久久久内射近拍高清| 精品久久久久久无码专区不卡| 色综合久久天天综合| 精品久久久久香蕉网| 99久久精品免费观看国产| 亚洲精品无码专区久久同性男| 久久久久亚洲AV无码网站| 久久久久久极精品久久久| 91精品日韩人妻无码久久不卡| 久久久久一本毛久久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久久亚洲裙底偷窥综合| 97超级碰碰碰碰久久久久| 香蕉久久永久视频| 久久99国产精一区二区三区| 久久婷婷激情综合色综合俺也去| 欧美久久精品一级c片片| 一本久久综合亚洲鲁鲁五月天| 91久久婷婷国产综合精品青草| 久久无码国产| 欧美精品一区二区精品久久 | 国产亚州精品女人久久久久久 | 精品无码久久久久久午夜| 久久伊人五月天论坛| 国产精品美女久久久m| 伊人久久大香线蕉综合热线| 97精品国产97久久久久久免费| 久久精品国产亚洲77777| 国内精品伊人久久久影院| 丁香久久婷婷国产午夜视频| 久久艹国产|