一、描述
概念:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合的使用具有一致性。
Composite 模式的典型结构图为:
图 2-1:Composite Pattern 结构图
二、实例:
大家在上学的时候应该都学过“数据结构”这门课程吧,还记得其中有一节叫“二叉树”吧,我们上学那会儿这一章节是必考内容,左子树,右子树,什么先序遍历后序遍历什么,重点就是二叉树的的遍历,我还记得当时老师就说,考试的时候一定有二叉树的构建和遍历,现在想起来还是觉的老师是正确的,树状结果在实际项目应用的非常广泛。
咱就先说个最常见的例子,公司的人事管理就是一个典型的树状结构,你想想你公司的结构是不是这样:
老大,往下一层一层的管理,最后到我们这层小兵,很典型的树状结构(说明一下,这不是二叉树,有关二叉树的定义可以翻翻以前的教科书),我们今天的任务就是要把这个树状结构实现出来,并且还要把它遍历一遍,你要确认你建立的树是否有问题呀。从这个树状结构上分析,有两种节点:有分支的节点(如研发部经理)和无分支的节点(如员工 A、员工 D 等),我们增加一点学术术语上去,总经理叫做根节点(是不是想到 XML 中的那个根节点 root,那就对了),类似研发部经理有分支的节点叫做树枝节点,类似员工 A 的无分支的节点叫做树叶节点,都很形象,三个类型的的节点,那是不是定义三个类就可以?好,我们按照这个思路走下去,先看我们自己设计的类图:
这个类图是初学者最容易想到的类图(这个类图有缺陷了),你有没有发觉有问题?getInfo 每个接口都有为什么不能抽象出来?Root 类和 Branch 类有什么差别?为什么要定义成两个接口两个类?如果我要加一个任职期限,你是不是每个类都需要修改?如果我要后序遍历(从员工找到他的上级领导)能做吗?——彻底晕菜了!
问题很多,我们一个一个解决,先说抽象的问题,确实可以吧 IBranch 和 IRoot 合并成一个接口,这个我们先肯定下来,这是个比较大的改动,我们先画个类图(类名稍微做下变化):
本人工程目录:
注释:
main(),客户
CCorpNode,抽象基类,实现基本信息
CBranchNode,树枝节点,实现Addordinate()函数和GetSubordinate()函数
CLeafNode,叶子节点,IsLeaf属性总是“true”
说明:组合模式主要是实现在CBranchNode对象里增加对其它对象的数组,如vector<CCorpNode*>,数组里可以存放CBranchNode和CLeafNode对象。这样方便进行遍历操作。
注意:组合模式有透明组合模式和安全组合模式。透明组合模式是将Addordinate和GetSubordinate这两个函数也抽象到CCorpNode基类里,这增加了操作叶子节点的难度,更易出现逻辑问题。所以尽量使用安全模式。
这个简单了,可以想像一下TreeView和TreeNode基本上是这个意思了,将很多数据组织在一块。
代码:
抽象基类,实现基本信息:CorpNode类
CorpNode.h
#ifndef __Composite__CorpNode__
#define __Composite__CorpNode__
#include <iostream>
using std::string;
class CCorpNode {
public:
CCorpNode(void);
CCorpNode(string _name,string _pos,string _salary);
virtual ~CCorpNode(void);
virtual string GetInfo();
void SetParent(CCorpNode *_pParent);
CCorpNode *GetParent();
virtual bool IsLeaf() = 0;
private:
string m_name;
string m_position;
string m_salary;
protected:
bool m_isLeaf;
CCorpNode *m_pParent;
};
#endif /* defined(__Composite__CorpNode__) */
CorpNode.cpp
#include "CorpNode.h"
CCorpNode::CCorpNode(void)
{
m_name = "";
m_position = "";
m_salary = "0";
}
CCorpNode::CCorpNode(string _name,string _pos,string _salary):m_name(_name), m_position(_pos), m_salary(_salary)
{
}
CCorpNode::~CCorpNode(void)
{
}
string CCorpNode::GetInfo()
{
string info = "";
info.append("姓名: ");
info.append(this->m_name);
info.append("\t职位:");
info.append(this->m_position);
info.append("\t薪水:");
info.append(this->m_salary);
return info;
}
void CCorpNode::SetParent(CCorpNode *_pParent)
{
this->m_pParent = _pParent;
}
CCorpNode * CCorpNode::GetParent()
{
return this->m_pParent;
}
树枝节点:BranchNode类
BranchNode.h
#ifndef __Composite__BranchNode__
#define __Composite__BranchNode__
#include <iostream>
#include "CorpNode.h"
#include <vector>
using std::vector;
using std::string;
class CBranchNode:public CCorpNode
{
public:
CBranchNode(void);
CBranchNode(string name,string pos,string salary);
~CBranchNode(void);
void Add(CCorpNode* pcorpNode);
vector<CCorpNode*> GetSubordinateInfo();
bool IsLeaf();
private:
vector<CCorpNode*> m_subordinateList;
};
#endif /* defined(__Composite__BranchNode__) */
BranchNode.cpp
#include "BranchNode.h"
CBranchNode::CBranchNode(void)
{
m_isLeaf = false;
}
CBranchNode::CBranchNode(string name ,string pos,string salary):CCorpNode(name,pos,salary)
{
m_isLeaf = false;
}
CBranchNode::~CBranchNode(void)
{
}
void CBranchNode::Add(CCorpNode *pcorpNode)
{
pcorpNode->SetParent(this);
m_subordinateList.push_back(pcorpNode);
}
vector<CCorpNode*> CBranchNode::GetSubordinateInfo()
{
return this->m_subordinateList;
}
bool CBranchNode::IsLeaf()
{
return m_isLeaf;
}
叶子节点:LeafNode类
LeafNode.h
#ifndef __Composite__LeafNode__
#define __Composite__LeafNode__
#include <iostream>
#include "CorpNode.h"
class CLeafNode:public CCorpNode
{
public:
CLeafNode(void);
CLeafNode(string name,string pos,string salary);
~CLeafNode(void);
bool IsLeaf();
};
#endif /* defined(__Composite__LeafNode__) */
LeafNode.cpp
#include "LeafNode.h"
CLeafNode::CLeafNode(void)
{
m_isLeaf = true;
}
CLeafNode::CLeafNode(string name ,string pos,string salary):CCorpNode(name,pos,salary)
{
m_isLeaf = true;
}
CLeafNode::~CLeafNode(void)
{
}
bool CLeafNode::IsLeaf()
{
return m_isLeaf;
}
客户:main主程序
main.cpp
#include <stdio.h>
#include "CorpNode.h"
#include "BranchNode.h"
#include "LeafNode.h"
#include "CConvert.h"
using std::cout;
string getTreeInfo(CBranchNode *root)
{
vector<CCorpNode*> subordinateList = root->GetSubordinateInfo();
string info = "";
for (vector<CCorpNode*>::iterator it = subordinateList.begin();it != subordinateList.end();it++)
{
if ((*it)->IsLeaf())
{
info = info.append(" "+(*it)->GetInfo()+"\n");
}
else
{
info = info.append((*it)->GetInfo()+"\n"+getTreeInfo((CBranchNode*)(*it)));
}
}
return info;
}
CBranchNode *compositeCorpTree()
{
//首先产生总经理CEO
CBranchNode *root = new CBranchNode("王大麻子","总经理","100000");
//把三个部门经理产生出来
CBranchNode *developDep = new CBranchNode("刘大瘸子","研发部门经理","10000");
CBranchNode *salesDep = new CBranchNode("马二拐子","销售部门经理","20000");
CBranchNode *financeDep = new CBranchNode("赵三驼子","财务部经理","10000");
//再把三个小组长产生出来
CBranchNode *firstDevGroup = new CBranchNode("杨三乜斜","开发一组组长","5000");
CBranchNode *secondDevGroup = new CBranchNode("吴大棒槌","开发二组组长","6000");
//把所有的小兵都产生出来
CLeafNode *a = new CLeafNode("a","开发人员","2000");
CLeafNode *b = new CLeafNode("b","开发人员","2000");
CLeafNode *c = new CLeafNode("c","开发人员","2000");
CLeafNode *d = new CLeafNode("d","开发人员","2000");
CLeafNode *e = new CLeafNode("e","开发人员","2000");
CLeafNode *f = new CLeafNode("f","开发人员","2000");
CLeafNode *g = new CLeafNode("g","开发人员","2000");
CLeafNode *h = new CLeafNode("h","销售人员","2000");
CLeafNode *i = new CLeafNode("i","销售人员","2000");
CLeafNode *j = new CLeafNode("i","财务人员","2000");
CLeafNode *k = new CLeafNode("k","CEO秘书","2000");
CLeafNode *zhengLaoLiu = new CLeafNode("郑老六","研发部副经理","2000");
//开始组装
//CEO下有三个部门经理和一个秘书
root->Add(developDep);
root->Add(salesDep);
root->Add(financeDep);
root->Add(k);
//研发部经理
developDep->Add(zhengLaoLiu);
developDep->Add(firstDevGroup);
developDep->Add(secondDevGroup);
//看看开发两个开发小组下有什么
firstDevGroup->Add(a);
firstDevGroup->Add(b);
firstDevGroup->Add(c);
secondDevGroup->Add(d);
secondDevGroup->Add(e);
secondDevGroup->Add(f);
//再看销售部下的人员情况
salesDep->Add(h);
salesDep->Add(i);
//最后一个财务
firstDevGroup->Add(j);
return root;
delete zhengLaoLiu,k,j,i,h,g,f,e,d,c,b,a,secondDevGroup,firstDevGroup,financeDep,salesDep,developDep,root;
}
int main(int argc, const char * argv[])
{
CBranchNode *CEO = compositeCorpTree();
cout<<CEO->GetInfo()<<std::endl;
cout<< getTreeInfo(CEO);
// insert code here...
//printf("Hello, World!\n");
return 0;
}
结果如下:
参考文献:《设计模式之禅》,《GoF_23种设计模式解析》
参考博客:http://www.cnblogs.com/wanggary/archive/2011/04/19/2021638.html