• <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
            來(lái)自于《大話設(shè)計(jì)模式》
            原型模式(Prototype):用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過(guò)拷貝這些原型創(chuàng)建新的對(duì)象。

            這個(gè)設(shè)計(jì)模式在 C++ 中如何實(shí)現(xiàn)?

            UML 圖:

            代碼實(shí)現(xiàn) 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 閱讀(303) 評(píng)論(0)  編輯 收藏 引用

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


            久久A级毛片免费观看| 亚洲国产精品成人久久蜜臀| 久久综合视频网站| 香蕉久久夜色精品国产尤物| 97精品依人久久久大香线蕉97 | 国产成人久久精品区一区二区| WWW婷婷AV久久久影片| 精品久久久久久国产三级| 久久精品免费全国观看国产| 久久精品嫩草影院| 久久人人爽人人爽人人片av麻烦| 99久久精品费精品国产一区二区 | 国产精品99精品久久免费| 久久国产精品无码网站| 亚洲色欲久久久综合网东京热| 日本福利片国产午夜久久| 亚洲国产一成人久久精品| 久久久久久A亚洲欧洲AV冫| 久久精品www| 久久久久久午夜成人影院| 亚洲伊人久久综合影院| 国产精品激情综合久久| 久久久久久久97| 久久夜色精品国产噜噜噜亚洲AV| 狠狠色伊人久久精品综合网 | 久久久久久伊人高潮影院| 精品久久人人妻人人做精品| 狠狠色婷婷久久一区二区三区| 精品久久久久久无码不卡| 精品国产91久久久久久久a| 久久99久久99小草精品免视看| 无码精品久久久久久人妻中字| 久久一区二区三区99| 久久久久人妻一区精品| 久久久久女教师免费一区| 久久国产精品波多野结衣AV| 久久国产精品免费| 亚洲人AV永久一区二区三区久久 | 伊人久久大香线蕉av一区| 亚洲精品乱码久久久久久蜜桃不卡| 久久婷婷是五月综合色狠狠|