• <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 閱讀(379) 評論(0)  編輯 收藏 引用 所屬分類: C++開發工具

            導航

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章檔案

            收藏夾

            Good blogs

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日日狠狠久久偷偷色综合96蜜桃| 久久人人爽人人人人片av| 久久精品国产亚洲综合色| 亚洲综合精品香蕉久久网97 | 久久国产热精品波多野结衣AV| 久久99精品久久久久久hb无码 | 久久久久久久久久久精品尤物| 蜜臀久久99精品久久久久久小说| 99久久精品免费| 国产精品无码久久久久久| 2020国产成人久久精品| 久久青青草视频| 精品久久久久久成人AV| 久久99热狠狠色精品一区| 久久久久国产视频电影| 一本色道久久99一综合| 久久久久国产一级毛片高清版| 少妇久久久久久被弄到高潮| 久久99精品久久久久婷婷| 久久综合亚洲色HEZYO社区| 久久www免费人成看国产片| 狠狠色丁香婷婷久久综合| 亚洲精品无码专区久久久| 国产精品久久自在自线观看| 色综合久久天天综线观看| 久久久国产打桩机| 久久午夜电影网| 久久久免费精品re6| 日本久久中文字幕| 久久九九久精品国产免费直播| 色老头网站久久网| 久久人人爽人人澡人人高潮AV| 看久久久久久a级毛片| 综合久久一区二区三区| 色婷婷噜噜久久国产精品12p| 久久精品国产亚洲麻豆| 亚洲精品国精品久久99热一| 久久香蕉国产线看观看精品yw| 国产高清国内精品福利99久久| 久久久噜噜噜久久中文福利| 久久国产精品77777|