[C++]组合模式

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

github源码路径:https://github.com/dangwei-90/Design-Mode

 // 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// 参考大话设计模式 - 组合模式

#include <iostream>
#include <vector>

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

using namespace std;

class Component {
public:
	Component() {}
	Component(string name) {
		name_ = name;
	}

	virtual void Add(Component* component) = 0;
	virtual void Remove(Component* component) = 0;
	virtual void Display(int depth) = 0;

public:
	string name_ = "";
};

// 叶子结点
class Leaf : public Component {
public:
	Leaf(string name) {
		name_ = name;
	}

	void Add(Component* component) {
		cout << "not have a component" << endl;
	}

	void Remove(Component* component) {
		cout << "not have a component" << endl;
	}

	void Display(int depth) {
		for (int n = 0; n < depth; n++) {
			cout << "-";
		}
		cout << name_ << endl;
	}; 
};

// 结点
class Composite : public Component {
public:
	Composite(string name) {
		name_ = name;
	}

	void Add(Component* component) {
		component_vector_.push_back(component);
	}

	void Remove(Component* component) {
		component_vector_.emplace_back(component);
	}

	// 遍历该结点,以及该结点拥有的子节点
	void Display(int depth) {
		for (int n = 0; n < depth; n++) {
			cout << "-";
		}
		cout << name_ << endl;

		for (auto iter = component_vector_.begin(); iter != component_vector_.end(); iter++) {
			(*iter)->Display(depth + 2);
		}
	}

private:
	vector<Component*> component_vector_;
};

int main()
{
	// 根节点及两个叶子结点
	Composite* root = new Composite("root");
	root->Add(new Leaf("leaf_a"));
	root->Add(new Leaf("leaf_b"));

	// 子结点一,及两个叶子结点
	Composite* comp_1 = new  Composite("comp_1");
	comp_1->Add(new Leaf("comp1_a"));
	comp_1->Add(new Leaf("comp1_b"));

	// 子结点一挂在根节点上
	root->Add(comp_1);
	
	// 子结点二,及两个叶子结点
	Composite* comp_2 = new  Composite("comp_2");
	comp_2->Add(new Leaf("comp2_a"));
	comp_2->Add(new Leaf("comp2_b"));

	// 子结点二挂在子结点一上
	comp_1->Add(comp_2);

	// 子结点三,及两个叶子结点
	Composite* comp_3 = new  Composite("comp_3");
	comp_3->Add(new Leaf("comp3_a"));
	comp_3->Add(new Leaf("comp3_b"));

	// 子结点三挂在根节点上
	root->Add(comp_3);

	// 从根节点开始遍历
	root->Display(1);

	SAFE_DELETE(comp_3);
	SAFE_DELETE(comp_2);
	SAFE_DELETE(comp_1);
	SAFE_DELETE(root);

	return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值