• <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
            來自于《大話設計模式》
            原型模式(Prototype):用原型實例指定創建對象的種類,并且通過拷貝這些原型創建新的對象。

            這個設計模式在 C++ 中如何實現?

            UML 圖:

            代碼實現 C++:
              1 #include <iostream>
              2 using namespace std;
              3 
              4 class Prototype
              5 {
              6 public:
              7     virtual Prototype& Clone() = 0;
              8 };
              9 
             10 class ConcretePrototype
             11 {
             12 private:
             13     int m;
             14     char* str;
             15 public:
             16     ConcretePrototype(int n = 0const char* s = "") : m(n)
             17     {
             18         str = new char[strlen(s) + 1];
             19         if (str == 0)
             20         {
             21             exit(1);
             22         }
             23         strcpy(str, s);
             24     }
             25     ConcretePrototype(const ConcretePrototype& p)
             26     {
             27         str = new char[strlen(p.str) + 1];
             28         if (str == 0)
             29         {
             30             exit(1);
             31         }
             32         strcpy(str, p.str);
             33         m = p.m;
             34     }
             35     ConcretePrototype& operator =(const ConcretePrototype& p)
             36     {
             37         if (this == &p)
             38         {
             39             delete [] str;
             40             str = new char[strlen(p.str) + 1];
             41             if (str == 0)
             42             {
             43                 exit(1);
             44             }
             45             strcpy(str, p.str);
             46             m = p.m;
             47         }
             48         return *this;
             49     }
             50     ~ConcretePrototype()
             51     {
             52         delete [] str;
             53     }
             54     ConcretePrototype& Clone()
             55     {
             56         return *this;
             57     }
             58     void SetStr(const char* s = "")
             59     {
             60         delete [] str;
             61         str = new char[strlen(s) + 1];
             62         if (str == 0)
             63         {
             64             exit(1);
             65         }
             66         strcpy(str, s);
             67     }
             68     void SetM(int n)
             69     {
             70         m = n;
             71     }
             72     void SetStrAndM(const char* s, int n)
             73     {
             74         delete [] str;
             75         str = new char[strlen(s) + 1];
             76         if (str == 0)
             77         {
             78             exit(1);
             79         }
             80         strcpy(str, s);
             81         m = n;
             82     }
             83     void test()
             84     {
             85         cout << m << endl;
             86         cout << str << endl;
             87     }
             88 };
             89 
             90 int main()
             91 {
             92     ConcretePrototype c(5"abc");
             93     c.test();
             94 
             95     ConcretePrototype c2 = static_cast<ConcretePrototype>(c.Clone());
             96     c2.test();
             97 
             98     c2.SetStrAndM("xyz"7);
             99     c2.test();
            100 
            101     return 0;
            102 
            103 }

            posted on 2011-04-25 16:03 unixfy 閱讀(309) 評論(0)  編輯 收藏 引用
            久久天天躁狠狠躁夜夜avapp| 69久久夜色精品国产69| 久久国产精品二国产精品| 久久99九九国产免费看小说| 久久久久亚洲av无码专区导航 | 中文字幕无码av激情不卡久久| 久久不射电影网| 精品无码久久久久久午夜| 欧美一区二区久久精品| 7国产欧美日韩综合天堂中文久久久久| 久久www免费人成看国产片| 99久久久国产精品免费无卡顿 | 欧美亚洲国产精品久久蜜芽| 色婷婷综合久久久中文字幕| 久久经典免费视频| 亚洲AV日韩AV天堂久久| 无码人妻精品一区二区三区久久久 | 国产婷婷成人久久Av免费高清| 久久精品日日躁夜夜躁欧美| 久久久久久伊人高潮影院| 亚洲精品国精品久久99热一| 久久99国产亚洲高清观看首页| 爱做久久久久久| 一本一道久久综合狠狠老 | 国产精品一久久香蕉产线看| 久久精品国产久精国产思思| 亚洲国产精品婷婷久久| 青青青国产成人久久111网站| 93精91精品国产综合久久香蕉 | 色狠狠久久AV五月综合| 久久久久人妻精品一区二区三区| 久久久这里只有精品加勒比| 久久精品中文字幕一区| 久久精品一本到99热免费| 伊人久久久AV老熟妇色| 亚洲av日韩精品久久久久久a | 久久久亚洲裙底偷窥综合| 久久久久久精品成人免费图片 | 久久久久久亚洲Av无码精品专口| 久久久无码精品亚洲日韩京东传媒 | 久久夜色tv网站|