• <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>

            積木

            No sub title

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            轉(zhuǎn)載自:http://patmusing.blog.163.com/blog/static/135834960201002321018760/


            在面向?qū)ο蟮南到y(tǒng)中,我們經(jīng)常會(huì)遇到一類(lèi)具有
            容器特征的對(duì)象,即它們?cè)诔洚?dāng)對(duì)象的同時(shí),又是其他對(duì)象的容器。

            舉例:

            在操作系統(tǒng)中,文件的概念很廣泛,其中文件可以是普通文件,也可以是目錄(Unix中,設(shè)備也是文件),目錄中可以存放文件。Composite設(shè)計(jì)模式就是將客戶(hù)代碼與復(fù)雜的對(duì)象容器結(jié)構(gòu)解耦,讓對(duì)象容器自己來(lái)實(shí)現(xiàn)自身的復(fù)雜結(jié)構(gòu),從而使得客戶(hù)代碼就像處理簡(jiǎn)單對(duì)象(文件)一樣來(lái)處理復(fù)雜的對(duì)象容器(目錄)。

            “Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.” – GoF

            10. C++實(shí)現(xiàn)Structural - Composite模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            調(diào)用Directory類(lèi)對(duì)象的process函數(shù),和調(diào)用PhysicalFile類(lèi)對(duì)象的process一樣簡(jiǎn)單。

            從上面的UML類(lèi)圖中,可以看出DirectoryFile這兩個(gè)類(lèi)之間的關(guān)系:

            1. Directory “is a”File

            2. Directory “has a(more)” File

            這是典型的遞歸結(jié)構(gòu)。因此在處理遞歸問(wèn)題時(shí),如果必要,可以考慮采用Composite模式。后面要講到的Decorator模式也是如此。

            // Composite.h

            #include <iostream>

            #include <list>

            using namespace std;

            class File

            {

            public:

            virtual void process() = 0;

            // 虛函數(shù):增加一個(gè)文件

            virtual void add(File* file)

            {

            }

            // 虛函數(shù):刪除一個(gè)文件

            virtual void remove(File* file)

            {

            }

            public:

            virtual ~File()

            {

            cout << "in the destructor of File..." << endl;

            }

            };

            // 葉子節(jié)點(diǎn)

            class PhysicalFile : public File

            {

            public:

            void process()

            {

            cout << "process() in PhysicalFile..." << endl;

            }

            public:

            ~PhysicalFile()

            {

            cout << "in the destructor of PhysicalFile..." << endl;

            }

            };

            // 容器節(jié)點(diǎn):Composite節(jié)點(diǎn)

            class Directory : public File

            {

            private:

            list<File*> file_list;

            public:

            Directory()

            {

            }

            void process()

            {

            cout << "process() in Directory..." << endl;

            if(!file_list.empty())

            {

            for(list<File*>::iterator it = file_list.begin(); it != file_list.end(); it++)

            {

            File* f = *it;

            f->process();

            }

            }

            }

            void add(File* file)

            {

            file_list.push_back(file);

            }

            void remove(File* file)

            {

            file_list.remove(file);

            }

            public:

            ~Directory()

            {

            cout << "in the destructor of Directory..." << endl;

            }

            };

            // Composite.cpp

            #include "Composite.h"

            int main(int argc, char **argv)

            {

            File *f1 = new Directory;

            File *f2 = new Directory;

            File *f3 = new PhysicalFile;

            f2->add(f3);

            f1->add(f2);

            File *f4 = new Directory;

            File *f5 = new Directory;

            File *f6 = new Directory;

            File *f7 = new PhysicalFile;

            f6->add(f7);

            f5->add(f6);

            f4->add(f5);

            f1->add(f4);

            f1->process();

            f1->remove(f4);

            cout << "+++++++++++++++++++++++" << endl;

            f1->process();

            // STL container中的元素是指針對(duì)象,那么必須手動(dòng)刪除。

            delete f1;

            delete f2;

            delete f3;

            delete f4;

            delete f5;

            delete f6;

            delete f7;

            return 0;

            }

            上述程序中,各對(duì)象之間的關(guān)系如下圖:

            10. C++實(shí)現(xiàn)Structural - Composite模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            其中f3f7PhysicalFile對(duì)象。f1包含了f2f4f2包含了f3f4包含了f5,f5包含了f6,f6包含了f7。

            運(yùn)行結(jié)果如下:

            process() in Directory... // f1

            process() in Directory... // f2

            process() in PhysicalFile... // f3

            process() in Directory... // f4

            process() in Directory... // f5

            process() in Directory... // f6

            process() in PhysicalFile... // f7

            +++++++++++++++++++++++ // 刪除f4后的輸出(可以看到f4及其包含的對(duì)象全部被刪除了)

            process() in Directory... // f1

            process() in Directory... // f2

            process() in PhysicalFile... // f3

            in the destructor of Directory...

            in the destructor of File...

            in the destructor of Directory...

            in the destructor of File...

            in the destructor of PhysicalFile...

            in the destructor of File...

            in the destructor of Directory...

            in the destructor of File...

            in the destructor of Directory...

            in the destructor of File...

            in the destructor of Directory...

            in the destructor of File...

            in the destructor of PhysicalFile...

            in the destructor of File...



            posted on 2013-03-07 22:33 Jacc.Kim 閱讀(226) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 設(shè)計(jì)模式
            亚洲午夜无码AV毛片久久| 久久久久国产一区二区| 精品国产乱码久久久久久呢| 久久综合给合综合久久| 欧美国产精品久久高清| 亚洲精品综合久久| 久久久免费精品re6| 久久成人国产精品二三区| 久久青青国产| 人妻无码精品久久亚瑟影视 | 日本加勒比久久精品| 亚洲欧洲中文日韩久久AV乱码| 久久天天婷婷五月俺也去| 丰满少妇人妻久久久久久| 精品久久综合1区2区3区激情| 美女久久久久久| 久久无码人妻一区二区三区| 9191精品国产免费久久| 亚洲综合日韩久久成人AV| 国内精品伊人久久久久| 久久婷婷五月综合色奶水99啪| 亚洲av伊人久久综合密臀性色| 九九久久精品国产| 国产精品一久久香蕉国产线看观看| 大香网伊人久久综合网2020| 伊人久久大香线蕉亚洲五月天 | 精品久久久久久久无码| 久久婷婷五月综合97色直播| 人妻精品久久无码区| 久久久噜噜噜久久中文字幕色伊伊 | 亚洲国产精品无码久久一区二区 | 久久99精品国产麻豆蜜芽| 激情伊人五月天久久综合| 久久人人爽人人爽人人片AV麻烦| 久久综合九色综合欧美狠狠| 三上悠亚久久精品| 久久亚洲sm情趣捆绑调教| 久久久精品国产| 一级a性色生活片久久无| 欧美日韩精品久久久久 | 狠狠色综合网站久久久久久久高清|