• <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 - 183,  comments - 10,  trackbacks - 0
            來自于《大話設計模式》
            如果需要添加其他操作,需要
            ·添加一個操作類
            ·修改 OperationFactory
            ·修改客戶代碼以使用這個操作

            UML 類圖如下:


            實例代碼 C++:
              1 #include <iostream>
              2 using namespace std;
              3 
              4 class Operation
              5 {
              6 protected:
              7     double _numberA;
              8     double _numberB;
              9 public:
             10     Operation(double a = 0.0double b = 0.0) : _numberA(a), _numberB(b) {}
             11     virtual ~Operation() {}
             12     virtual double GetResult()
             13     {
             14         double result = 0.0;
             15         return result;
             16     }
             17 };
             18 
             19 class OperationAdd : public Operation
             20 {
             21 public:
             22     OperationAdd(double a = 0.0double b = 0.0) : Operation(a, b) {}
             23     virtual double GetResult()
             24     {
             25         return _numberA + _numberB;
             26     }
             27 };
             28 
             29 class OperationSub : public Operation
             30 {
             31 public:
             32     OperationSub(double a = 0.0double b = 0.0) : Operation(a, b) {}
             33     virtual double GetResult()
             34     {
             35         return _numberA - _numberB;
             36     }
             37 };
             38 
             39 class OperationMul : public Operation
             40 {
             41 public:
             42     OperationMul(double a = 0.0double b = 0.0) : Operation(a, b) {}
             43     virtual double GetResult()
             44     {
             45         return _numberA * _numberB;
             46     }
             47 };
             48 
             49 class OperationDiv : public Operation
             50 {
             51 public:
             52     OperationDiv(double a = 0.0double b = 0.0) : Operation(a, b) {}
             53     virtual double GetResult()
             54     {
             55         if (_numberB == 0.0)
             56         {
             57             throw runtime_error("Denominator is 0!");
             58         }
             59         return _numberA / _numberB;
             60     }
             61 };
             62 
             63 class OperationFactory
             64 {
             65 public:
             66     static Operation* CreateOperation(char ope, double a = 0.0double b = 0.0)
             67     {
             68         Operation* ret = 0;
             69         switch (ope)
             70         {
             71         case '+':
             72             ret = new OperationAdd(a, b);
             73             break;
             74         case '-':
             75             ret = new OperationSub(a, b);
             76             break;
             77         case '*':
             78             ret = new OperationMul(a, b);
             79             break;
             80         case '/':
             81             ret = new OperationDiv(a, b);
             82             break;
             83         default:
             84             break;
             85         }
             86         return ret;
             87     }
             88 };
             89 
             90 int main()
             91 {
             92     try
             93     {
             94         Operation* p_ope;
             95         p_ope = OperationFactory::CreateOperation('+'3.05.0);
             96         if (p_ope != 0)
             97         {
             98             cout << p_ope->GetResult() << endl;
             99             delete p_ope;
            100             p_ope = 0;
            101         }
            102 
            103         p_ope = OperationFactory::CreateOperation('-'3.05.0);
            104         if (p_ope != 0)
            105         {
            106             cout << p_ope->GetResult() << endl;
            107             delete p_ope;
            108             p_ope = 0;
            109         }
            110 
            111         p_ope = OperationFactory::CreateOperation('*'3.05.0);
            112         if (p_ope != 0)
            113         {
            114             cout << p_ope->GetResult() << endl;
            115             delete p_ope;
            116             p_ope = 0;
            117         }
            118 
            119         p_ope = OperationFactory::CreateOperation('/'3.05.0);
            120         if (p_ope != 0)
            121         {
            122             cout << p_ope->GetResult() << endl;
            123             delete p_ope;
            124             p_ope = 0;
            125         }
            126 
            127         p_ope = OperationFactory::CreateOperation('/'3.00.0);
            128         if (p_ope != 0)
            129         {
            130             cout << p_ope->GetResult() << endl;
            131             delete p_ope;
            132             p_ope = 0;
            133         }
            134     }
            135     catch (const exception& e)
            136     {
            137         cerr << e.what() << endl;
            138     }
            139     return 0;
            140 }
            posted on 2011-04-21 14:19 unixfy 閱讀(232) 評論(0)  編輯 收藏 引用
            久久亚洲美女精品国产精品| 欧美一级久久久久久久大| 午夜精品久久久久久久| 97久久超碰国产精品2021| 热re99久久精品国产99热| 色偷偷88欧美精品久久久| 7777精品久久久大香线蕉| 99久久国产主播综合精品| 久久久久青草线蕉综合超碰| 久久久久久久综合日本亚洲| 亚洲精品视频久久久| 欧美一区二区精品久久| 久久精品国产亚洲av麻豆蜜芽| 91亚洲国产成人久久精品网址| 久久人妻无码中文字幕| 国产精品免费看久久久香蕉| 97久久国产综合精品女不卡 | 亚洲精品乱码久久久久久蜜桃不卡| 久久激情五月丁香伊人| 国产成人久久AV免费| 久久精品国产亚洲AV久| 污污内射久久一区二区欧美日韩 | 激情五月综合综合久久69| 亚洲AV成人无码久久精品老人 | 国产aⅴ激情无码久久| 久久97久久97精品免视看秋霞| 国产精品99精品久久免费| 久久久久久毛片免费播放| 亚洲午夜久久久久妓女影院| 亚洲欧美国产精品专区久久| 久久久久国产精品三级网| 久久久精品无码专区不卡| 国内精品久久久久久久coent| 久久九九青青国产精品| 2021少妇久久久久久久久久| 99久久久精品| 女人香蕉久久**毛片精品| 99久久亚洲综合精品成人| 久久高清一级毛片| 久久久久人妻一区精品| 欧美久久久久久|