• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉載自:http://patmusing.blog.163.com/blog/static/135834960201002321018760/


            在面向對象的系統中,我們經常會遇到一類具有
            容器特征的對象,即它們在充當對象的同時,又是其他對象的容器。

            舉例:

            在操作系統中,文件的概念很廣泛,其中文件可以是普通文件,也可以是目錄(Unix中,設備也是文件),目錄中可以存放文件。Composite設計模式就是將客戶代碼與復雜的對象容器結構解耦,讓對象容器自己來實現自身的復雜結構,從而使得客戶代碼就像處理簡單對象(文件)一樣來處理復雜的對象容器(目錄)

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

            10. C++實現Structural - Composite模式 - 玄機逸士 - 玄機逸士博客

            調用Directory類對象的process函數,和調用PhysicalFile類對象的process一樣簡單。

            從上面的UML類圖中,可以看出DirectoryFile這兩個類之間的關系:

            1. Directory “is a”File

            2. Directory “has a(more)” File

            這是典型的遞歸結構。因此在處理遞歸問題時,如果必要,可以考慮采用Composite模式。后面要講到的Decorator模式也是如此。

            // Composite.h

            #include <iostream>

            #include <list>

            using namespace std;

            class File

            {

            public:

            virtual void process() = 0;

            // 虛函數:增加一個文件

            virtual void add(File* file)

            {

            }

            // 虛函數:刪除一個文件

            virtual void remove(File* file)

            {

            }

            public:

            virtual ~File()

            {

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

            }

            };

            // 葉子節點

            class PhysicalFile : public File

            {

            public:

            void process()

            {

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

            }

            public:

            ~PhysicalFile()

            {

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

            }

            };

            // 容器節點:Composite節點

            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中的元素是指針對象,那么必須手動刪除。

            delete f1;

            delete f2;

            delete f3;

            delete f4;

            delete f5;

            delete f6;

            delete f7;

            return 0;

            }

            上述程序中,各對象之間的關系如下圖:

            10. C++實現Structural - Composite模式 - 玄機逸士 - 玄機逸士博客

            其中f3f7PhysicalFile對象。f1包含了f2f4f2包含了f3f4包含了f5f5包含了f6f6包含了f7

            運行結果如下:

            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及其包含的對象全部被刪除了)

            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 閱讀(224) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            国产精品一区二区久久精品| 欧美精品一区二区久久| 国产精品久久久久久吹潮| 九九久久99综合一区二区| 久久99亚洲综合精品首页| 亚洲一本综合久久| 久久久久精品国产亚洲AV无码| 人妻少妇久久中文字幕| 久久精品国产亚洲7777| 久久午夜羞羞影院免费观看| 91精品国产91久久| 婷婷久久久亚洲欧洲日产国码AV| 国产福利电影一区二区三区,免费久久久久久久精 | 久久久久99这里有精品10| 久久亚洲欧美国产精品| 久久99精品久久久久久9蜜桃| 奇米影视7777久久精品人人爽| 91视频国产91久久久| 一本久久a久久精品综合香蕉| 久久久久久免费一区二区三区| 亚洲午夜精品久久久久久浪潮| 韩国无遮挡三级久久| 久久久久久亚洲Av无码精品专口| 国产三级观看久久| 1000部精品久久久久久久久| 亚洲国产精品无码成人片久久| 久久综合九色综合久99| 久久久99精品成人片中文字幕 | 色婷婷噜噜久久国产精品12p| 精品国产91久久久久久久 | 久久亚洲天堂| 久久久中文字幕日本| 久久www免费人成看国产片| 久久综合综合久久97色| 亚洲国产精品久久久久婷婷软件| 99久久国产综合精品麻豆| 97久久超碰国产精品旧版 | 欧美国产成人久久精品| 精品视频久久久久| 香蕉久久AⅤ一区二区三区| 日韩影院久久|