23种设计模式之Composite模式

第二部分:结构型模式

1. Composite模式

在开发中,我们经常可能要递归构建树状的组合结构,Composite模式则提供了很好的解决方案。

2. 结构图

在这里插入图片描述

3. 实例

client

#include "composite.h"
#include "leaf.h"

int main()
{
	// 创建一个树形结构
	// 创建根节点
	Component *pRoot = new Composite("江湖公司(任我行)");

	// 创建分支
	Component *pDepart1 = new Composite("日月神教(东方不败)");
	pDepart1->Add(new Leaf("光明左使(向问天)"));
	pDepart1->Add(new Leaf("光明右使(曲洋)"));
	pRoot->Add(pDepart1);

	Component *pDepart2 = new Composite("五岳剑派(左冷蝉)");
	pDepart2->Add(new Leaf("嵩山(左冷蝉)"));
	pDepart2->Add(new Leaf("衡山(莫大)"));
	pDepart2->Add(new Leaf("华山(岳不群)"));
	pDepart2->Add(new Leaf("泰山(天门道长)"));
	pDepart2->Add(new Leaf("恒山(定闲师太)"));
	pRoot->Add(pDepart2);

	// 添加和删除叶子
	pRoot->Add(new Leaf("少林(方证大师)"));
	pRoot->Add(new Leaf("武当(冲虚道长)"));
	Component *pLeaf = new Leaf("青城(余沧海)");
	pRoot->Add(pLeaf);

	// 小丑,直接裁掉
	pRoot->Remove(pLeaf);

	// 递归地显示组织架构
	pRoot->Operation(1);

	// 删除分配的内存
	SAFE_DELETE(pRoot);

	getchar();

	return 0;
}

component.h

#ifndef COMPONENT_H
#define COMPONENT_H

#include <iostream>
#include <string>

using namespace std;

class Component
{
public:
	Component(string name) : m_strName(name) {}
	virtual ~Component() {}
	virtual void Add(Component *cmpt) = 0;  // 添加构件
	virtual void Remove(Component *cmpt) = 0;  // 删除构件
	virtual Component* GetChild(int index) = 0;  // 获取构件
	virtual void Operation(int indent) = 0;  // 显示构件(以缩进的方式)
	
	Component()=delete;  // 不允许

protected:
	string m_strName;
};

#endif // COMPONENT_H
#ifndef COMPOSITE_H
#define COMPOSITE_H

#include <vector>
#include "component.h"

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
#endif

class Composite : public Component
{
public:
	Composite(string name) : Component(name) {}
	virtual ~Composite() {
		while (!m_elements.empty()) {
			vector<Component*>::iterator it = m_elements.begin();
			SAFE_DELETE(*it);
			m_elements.erase(it);
		}
	}
	void Add(Component *cmpt) {
		m_elements.push_back(cmpt);
	}
	void Remove(Component *cmpt) {
		vector<Component*>::iterator it = m_elements.begin();
		while (it != m_elements.end()) {
			if (*it == cmpt) {
				SAFE_DELETE(cmpt);
				m_elements.erase(it);
				break;
			}
			++it;
		}
	}
	Component* GetChild(int index) {
		if (index >= m_elements.size())
			return NULL;

		return m_elements[index];
	}
	// 递归显示
	void Operation(int indent) {
		string newStr(indent, '-');
		cout << newStr << "+ " << m_strName << endl;
		// 显示每个节点的孩子
		vector<Component*>::iterator it = m_elements.begin();
		while (it != m_elements.end()) {
			(*it)->Operation(indent + 2);
			++it;
		}
	}

private:
	Composite();  // 不允许

private:
	vector<Component *> m_elements;
};

#endif // COMPOSITE_H
// 透明组合模式
#ifndef LEAF_H
#define LEAF_H

#include "component.h"

class Leaf : public Component
{
public:
	Leaf(string name) : Component(name) {}
	virtual ~Leaf() {}
	void Add(Component *cmpt) {
		cout << "Can't add to a Leaf" << endl;
	}
	void Remove(Component *cmpt) {
		cout << "Can't remove from a Leaf" << endl;
	}
	Component* GetChild(int index) {
		cout << "Can't get child from a Leaf" << endl;
		return NULL;
	}
	void Operation(int indent) {
		string newStr(indent, '-');
		cout << newStr << " " << m_strName << endl;
	}

private:
	Leaf();  // 不允许
};

#endif // LEAF_H

4. 讨论

Composite模式在实现中有一个问题就是要提供对于子节点(Leaf)的管理策略,这里使用的是STL 中的vector,可以提供其他的实现方式,如数组、链表、Hash表等。
容易在组合体内增加对象部件。客户端不必因加入了新的部件而更改代码。有利于功能的扩展。
定义了“部分-整体”的层次结构,基本对象可以被组合成更大的对象,而且这种操作是可重复的,不断重复下去就可以得到一个非常大的组合对象,但这些组合对象与基本对象拥有相同的接口,因而组合是透明的,用法完全一致。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值