• <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 - 48,  comments - 21,  trackbacks - 0
            意圖:
            將對象組合成樹形結構以表示部分整體的層次結構。Composite使得用戶對單一對象和組合對象的使用具有一致性
            UML圖:

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

            // test.cpp : Defines the entry point for the console application.
            //

            #include 
            "stdafx.h"
            #include 
            "test.h"
            #include 
            <iostream>
            #include 
            <algorithm>

            using namespace std;
            //////////////////////////////////////////////////////////////////////////
            void Component::Add(Component* pChild)
            {
            }

            void Component::Remove(Component* pChild)
            {
            }

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

            //////////////////////////////////////////////////////////////////////////
            void Leaf::Operation()
            {
                cout 
            << "Operation of leaf\n";    
            }

            //////////////////////////////////////////////////////////////////////////
            Composite::~Composite()
            {
                std::list
            <Component*>::iterator iter1,iter2,temp;
                
                
            for (iter1 = m_ListOfComponent.begin(),iter2 = m_ListOfComponent.end(); iter1 != iter2; ++iter1)
                
            {
                    temp 
            = 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()
            {
                cout 
            << "Operation of Composite\n";
                list
            <Component*>::iterator iter1,iter2;
                
            for (iter1 = m_ListOfComponent.begin(),iter2 = m_ListOfComponent.end(); iter1 != iter2; ++iter1)
                
            {
                    (
            *iter1)->Operation();
                }

            }

            //////////////////////////////////////////////////////////////////////////
            int main(int argc, char* argv[])
            {
                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 2008-08-18 12:22 黑色天使 閱讀(1787) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式

            <2008年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产精品一区| 精产国品久久一二三产区区别| 亚洲国产天堂久久久久久| 成人精品一区二区久久久| 久久青草国产手机看片福利盒子| 欧洲成人午夜精品无码区久久| 久久综合偷偷噜噜噜色| 久久99热这里只频精品6| 久久露脸国产精品| 久久强奷乱码老熟女| 久久久久国产视频电影| 日日狠狠久久偷偷色综合0 | 香蕉久久久久久狠狠色| 久久人人超碰精品CAOPOREN| 久久影院久久香蕉国产线看观看| 久久久久九国产精品| 久久中文字幕无码专区| 九九精品久久久久久噜噜| 久久久久久曰本AV免费免费| 久久精品国产亚洲精品2020| 香蕉久久夜色精品升级完成| 久久久久亚洲AV片无码下载蜜桃| 99久久99久久| 久久久久亚洲精品男人的天堂| 色播久久人人爽人人爽人人片aV| 国产成人久久精品一区二区三区 | 91精品国产91久久久久久| 日本道色综合久久影院| 久久婷婷午色综合夜啪| 久久久久久久亚洲Av无码| 国产精品日韩欧美久久综合| 久久99精品国产麻豆婷婷| 久久久久av无码免费网| 久久精品国产91久久综合麻豆自制 | 亚洲欧美国产精品专区久久| 无码AV中文字幕久久专区| 国产99久久久久久免费看| 久久人人爽人人人人片av| 91亚洲国产成人久久精品| 久久人人添人人爽添人人片牛牛| 久久香蕉国产线看观看99|