emptysoul

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

常用链接

留言簿(18)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

组合模式(Composite)的定义为:组合多个对象形成树形结构,以表示整体-部分的结构层次。组合模式对单个对象和组合对象的使用具有一致性。其结构图如下:
 

例如一个新闻的树形菜单,它包含很多原始菜单(如国内、国际),以及由其子菜单组成的组合节点(如国内新闻下的时事、社会等),结构图如下:


实现代码:
//Menu.h
#include <string>

class Menu  
{
public:
    
virtual ~Menu();

    
virtual void Add(Menu*);
    
virtual void Remove(Menu*);
    
virtual Menu* GetChild(int);
    
virtual void Display() = 0;
protected:
    Menu();
    Menu(std::
string);
    std::
string m_strName;
};

//Menu.cpp
#include "stdafx.h"
#include 
"Menu.h"

Menu::Menu()
{

}

Menu::Menu(std::
string strName) : m_strName(strName)
{

}

Menu::
~Menu()
{

}

void Menu::Add(Menu* pMenu)
{}

void Menu::Remove(Menu* pMenu)
{}

Menu
* Menu::GetChild(int index)
{
    
return NULL;
}

//SubMenu.h
#include "Menu.h"

class SubMenu : public Menu  
{
public:
    SubMenu();
    SubMenu(std::
string);
    
virtual ~SubMenu();

    
void Display();
};

//SubMenu.cpp
#include "stdafx.h"
#include 
"SubMenu.h"
#include 
<iostream>

using namespace std;

SubMenu::SubMenu()
{

}

SubMenu::SubMenu(
string strName) : Menu(strName)
{

}

SubMenu::
~SubMenu()
{

}

void SubMenu::Display()
{
    cout 
<< m_strName << endl;
}

//CompositMenu.h
#include "Menu.h"
#include 
<vector>

class CompositMenu : public Menu
{
public:
    CompositMenu();
    CompositMenu(std::
string);
    
virtual ~CompositMenu();

    
void Add(Menu*);
    
void Remove(Menu*);
    Menu
* GetChild(int);
    
void Display();
private:
    std::vector
<Menu*> m_vMenu;
};

//CompositMenu.cpp
#include "stdafx.h"
#include 
"CompositMenu.h"
#include 
<iostream>

using namespace std;

CompositMenu::CompositMenu()
{
    
}

CompositMenu::CompositMenu(
string strName) : Menu(strName)
{

}

CompositMenu::
~CompositMenu()
{

}

void CompositMenu::Add(Menu* pMenu)
{
    m_vMenu.push_back(pMenu);
}

void CompositMenu::Remove(Menu* pMenu)
{
    m_vMenu.erase(
&pMenu);
}

Menu
* CompositMenu::GetChild(int index)
{
    
return m_vMenu[index];
}

void CompositMenu::Display()
{
    cout 
<< "+" << m_strName << endl;
    vector
<Menu*>::iterator it = m_vMenu.begin();
    
for (; it != m_vMenu.end(); ++it)
    {
        cout 
<< "|-";
        (
*it)->Display();
    }
}

#include 
"stdafx.h"
#include 
"Menu.h"
#include 
"SubMenu.h"
#include 
"CompositMenu.h"

int main(int argc, char* argv[])
{
    Menu
* pMenu = new CompositMenu("国内新闻");
    pMenu
->Add(new SubMenu("时事新闻"));
    pMenu
->Add(new SubMenu("社会新闻"));
    pMenu
->Display();
    pMenu 
= new CompositMenu("国际新闻");
    pMenu
->Add(new SubMenu("国际要闻"));
    pMenu
->Add(new SubMenu("环球视野"));
    pMenu
->Display();

    
return 0;
}

最后输出为:
+国内新闻
|-时事新闻
|-社会新闻
+国际新闻
|-国际要闻
|-环球视野
posted on 2009-02-11 19:51 emptysoul 阅读(2033) 评论(3)  编辑 收藏 引用

Feedback

# re: 设计模式-组合模式 2009-07-09 13:58 lu-bei
嘿,例子很棒,只是存在内存泄露^.^

Thanks for your sample!  回复  更多评论
  

# re: 设计模式-组合模式[未登录] 2011-11-29 22:49 jemmyLiu
你的其他的设计模式我都看过了 实现的挺简单明了的 就是没有释放内存的习惯 像java转过来的 呵呵  回复  更多评论
  

# re: 设计模式-组合模式 2012-08-27 20:12 江南烟雨
@lu-bei
只需要在CompositeMenu的析构函数中delete vector中的各个指针就可以了~  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理