• <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) 沒(méi)有判斷除0的條件
            (
            4) 沒(méi)有考慮到用戶輸入出錯(cuò)的問(wèn)題

            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
            通過(guò)封裝、繼承、多態(tài)把程序的耦合度降低,用設(shè)計(jì)模式使得程序更加的靈活,容易修改,并且易于復(fù)用。
            1.7
            小菜: 復(fù)制還是復(fù)用,這是個(gè)問(wèn)題。
            大鳥(niǎo): 問(wèn)題你個(gè)頭,當(dāng)然是復(fù)用啦!
            1.8
            業(yè)務(wù)邏輯應(yīng)該與界面邏輯分開(kāi),讓它們之間的耦合度降低。
            分開(kāi)才容易維護(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 閱讀(186) 評(píng)論(0)  編輯 收藏 引用

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



            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿

            文章分類

            文章檔案

            收藏夾

            搜索

            •  

            最新評(píng)論

            久久亚洲精品无码观看不卡| 日韩亚洲欧美久久久www综合网| 久久婷婷五月综合成人D啪| 久久免费视频6| 7777精品久久久大香线蕉| 大美女久久久久久j久久| 亚洲国产精品综合久久一线| 99久久婷婷免费国产综合精品| 久久国产成人午夜AV影院| 精产国品久久一二三产区区别| 久久福利青草精品资源站免费| 久久毛片免费看一区二区三区| 99久久99久久| 亚洲午夜久久久影院伊人| 久久久久九国产精品| 久久综合丝袜日本网| 久久夜色精品国产噜噜亚洲AV| 日本精品久久久久影院日本| 成人免费网站久久久| 伊人久久大香线蕉综合影院首页| 国产精品99久久久久久董美香 | 国产成人无码久久久精品一| 三级片免费观看久久| 国产成人久久777777| 久久九九有精品国产23百花影院| 精品久久久无码21p发布| 亚洲国产精品无码久久青草| 一本色道久久88加勒比—综合| 久久精品国产第一区二区三区| 伊人久久久AV老熟妇色| 精品国产青草久久久久福利| 狠狠综合久久AV一区二区三区| 亚洲国产一成久久精品国产成人综合 | 人妻精品久久久久中文字幕69| 色悠久久久久久久综合网| 久久精品无码一区二区app| 国产精品综合久久第一页| 国产福利电影一区二区三区,免费久久久久久久精 | 精品国产乱码久久久久久1区2区| 无码人妻久久久一区二区三区 | 久久久国产精品亚洲一区|