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

            來吧,朋友!

            為C++瘋狂

            設計模式解析和實現(C++) Composite模式

            作用:

                將對象組合成樹形結構以表示“部分-整體”的層次結構。Composite使得用戶對單個對象和組合對象的使用具有一致性。

                UML結構圖:



                抽象基類:

                1)Component:為組合中的對象聲明接口,聲明了類共有接口的缺省行為(如這里的Add,Remove,GetChild函數),聲明一個接口函數可以訪問Component的子組件。

                接口函數:

                1)Component::Operatation:定義了各個組件共有的行為接口,由各個組件的具體實現。

                2)Component::Add添加一個子組件

                3)Component::Remove::刪除一個子組件。

                4)Component::GetChild:獲得子組件的指針。

                解析:

                Component模式是為解決組件之間的遞歸組合提供了解決的辦法,它主要分為兩個派生類,其中的Leaf是葉子結點,也就是不含有子組件的結點,而Composite是含有子組件的類。舉一個例子來說明這個模式,在UI的設計中,最基本的控件是諸如Button,Edit這樣的控件,相當于是這里的Leaf組件,而比較復雜的控件比如List則可也看做是由這些基本的組件組合起來的控件,相當于這里的Composite,它們之間有一些行為含義是相同的,比如在控件上作一個點擊,移動操作等等的,這些都可以定義為抽象基類中的接口虛函數,由各個派生類去實現之,這些都會有的行為就是這里的Operation函數,而添加,刪除等進行組件組合的操作只有非葉子結點才可能有,所以虛擬基類中只是提供接口而且默認的實現是什么都不做。

            實現:

                1)Composite.h

             /**//********************************************************************
                created:    2006/07/20
                filename:     Composite.h
                author:        李創
                            http://m.shnenglu.com/converse/

                purpose:    Composite模式的演示代碼
            *********************************************************************/

            #ifndef COMPOSITE_H
            #define COMPOSITE_H

            #include <list>

            // 組合中的抽象基類
            class Component
            {
            public:
                Component(){}
                virtual ~Component(){}

                // 純虛函數,只提供接口,沒有默認的實現
                virtual void Operation() = 0;

                // 虛函數,提供接口,有默認的實現就是什么都不做
                virtual void Add(Component* pChild);
                virtual void Remove(Component* pChild);
                virtual Component* GetChild(int nIndex);
            };

            // 派生自Component,是其中的葉子組件的基類
            class Leaf
                : public Component
            {
            public:
                Leaf(){}
                virtual ~Leaf(){}

                virtual void Operation();
            };

            // 派生自Component,是其中的含有子件的組件的基類
            class Composite
                : public Component
            {
            public:
                Composite(){}
                virtual ~Composite();

                virtual void Operation();

                virtual void Add(Component* pChild);
                virtual void Remove(Component* pChild);
                virtual Component* GetChild(int nIndex);

            private:
                // 采用list容器去保存子組件
                std::list<Component*>    m_ListOfComponent;
            };

            #endif

            2)Composite.cpp

             /**//********************************************************************
                created:    2006/07/20
                filename:     Composite.cpp
                author:        李創
                            http://m.shnenglu.com/converse/

                purpose:    Composite模式的演示代碼
            *********************************************************************/

            #include "Composite.h"
            #include <iostream>
            #include <algorithm>

            /**//*-------------------------------------------------------------------
                Component成員函數的實現

             -------------------------------------------------------------------*/
            void Component::Add(Component* pChild)
            {

            }

            void Component::Remove(Component* pChild)
            {

            }

            Component* Component::GetChild(int nIndex)
            {
                return NULL;
            }

            /**//*-------------------------------------------------------------------
                Leaf成員函數的實現

            -------------------------------------------------------------------*/
            void Leaf::Operation()
            {
                std::cout << "Operation by leafn";
            }

            /**//*-------------------------------------------------------------------
                Composite成員函數的實現

            -------------------------------------------------------------------*/
            Composite::~Composite()
            {
                std::list<Component*>::iterator iter1, iter2, temp;

                for (iter1  = m_ListOfComponent.begin(), iter2 = m_ListOfComponent.end();
                     iter1 != iter2;
                     )
                {
                    temp = iter1;
                    ++iter1;
                    delete (*temp);
                }
            }

            void Composite::Add(Component* pChild)
            {
                m_ListOfComponent.push_back(pChild);
            }

            void Composite::Remove(Component* pChild)
            {
                std::list<Component*>::iterator iter;

                iter = find(m_ListOfComponent.begin(), m_ListOfComponent.end(), pChild);

                if (m_ListOfComponent.end() != iter)
                {
                    m_ListOfComponent.erase(iter);
                }
            }

            Component* Composite::GetChild(int nIndex)
            {
                if (nIndex <= 0 || nIndex > m_ListOfComponent.size())
                    return NULL;

                std::list<Component*>::iterator iter1, iter2;
                int i;
                for (i = 1, iter1  = m_ListOfComponent.begin(), iter2 = m_ListOfComponent.end();
                    iter1 != iter2;
                    ++iter1, ++i)
                {
                    if (i == nIndex)
                        break;
                }

                return *iter1;
            }

            void Composite::Operation()
            {
                std::cout << "Operation by Compositen";

                std::list<Component*>::iterator iter1, iter2;

                for (iter1  = m_ListOfComponent.begin(), iter2 = m_ListOfComponent.end();
                    iter1 != iter2;
                    ++iter1)
                {
                    (*iter1)->Operation();
                }
            }

            3)Main.cpp

             /**//********************************************************************
                created:    2006/07/20
                filename:     Main.cpp
                author:        李創
                            http://m.shnenglu.com/converse/

                purpose:    Composite模式的測試代碼
            *********************************************************************/

            #include "Composite.h"
            #include <stdlib.h>

            int main()
            {
                Leaf *pLeaf1 = new Leaf();
                Leaf *pLeaf2 = new Leaf();

                Composite* pComposite = new Composite;
                pComposite->Add(pLeaf1);
                pComposite->Add(pLeaf2);
                pComposite->Operation();
                pComposite->GetChild(2)->Operation();

                delete pComposite;

                system("pause");

                return 0;
            }

            posted on 2009-07-20 17:14 yanghaibao 閱讀(373) 評論(0)  編輯 收藏 引用 所屬分類: C++開發工具

            導航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章檔案

            收藏夾

            Good blogs

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久亚洲AV无码网站| 久久亚洲精品无码AV红樱桃| 亚洲va国产va天堂va久久| 久久婷婷国产剧情内射白浆 | 麻豆成人久久精品二区三区免费| 久久精品国产亚洲AV久| 久久精品国产亚洲av水果派| 国产高清国内精品福利99久久| 久久久艹| 狠狠色丁香婷婷久久综合不卡| 久久99精品久久久久久秒播| 亚洲国产美女精品久久久久∴ | 漂亮人妻被黑人久久精品| 久久精品国产91久久综合麻豆自制 | 99精品国产在热久久无毒不卡| 久久精品国产精品国产精品污| 国产精品va久久久久久久| 久久久久亚洲AV成人网人人网站 | 亚洲国产成人久久精品影视| 久久亚洲精品无码观看不卡| 亚洲色欲久久久综合网东京热 | 青青草原精品99久久精品66| 精品人妻伦九区久久AAA片69| 久久久无码精品亚洲日韩蜜臀浪潮| 久久这里只有精品久久| 亚洲国产一成人久久精品| 欧美一区二区久久精品| 久久国产精品-国产精品| 一本色道久久88—综合亚洲精品| 久久精品一区二区影院 | 久久青青草原精品影院| 99久久无色码中文字幕人妻| 久久丝袜精品中文字幕| 91精品婷婷国产综合久久| 国产精品岛国久久久久| 久久精品国产亚洲AV高清热| 久久精品国产99国产精品导航 | 久久99精品久久久久久动态图| 漂亮人妻被中出中文字幕久久 | 99久久人妻无码精品系列蜜桃| 伊人久久精品无码av一区|