团队管理——组合模式

组合模式

在这里插入图片描述

组合模式UML

组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构。

#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <Windows.h>

using namespace std;

//层级抽象节点
class abstruct_node
{
public:
	virtual void add(abstruct_node* node) = 0;
	virtual void remove(abstruct_node* node) = 0;
	virtual void show() = 0;
};

//高级子节点
class H_child_node : public abstruct_node
{
public:
	H_child_node(string n) :name(n) { table.clear(); }

	virtual void add(abstruct_node* node)
	{
		if (NULL != node)
			table.push_back(node);
	}

	virtual void remove(abstruct_node* node)
	{
		vector<abstruct_node*>::iterator iter = table.begin();
		for(; iter != table.end(); iter++)
		{
			if ((*iter) == node)
			{
				iter = table.erase(iter);
				break;
			}
			
		}
	}

	virtual void show()
	{
		cout << name << endl;

		vector<abstruct_node*>::iterator iter = table.begin();
		for (; iter != table.end(); iter++)
		{
			(*iter)->show();
		}
	}

private:
	vector<abstruct_node*> table;
	string name;
};

//低级子节点
class L_child_node : public abstruct_node
{
public:
	L_child_node(string n) :name(n) {}
	virtual void add(abstruct_node* node) {}
	virtual void remove(abstruct_node* node) {}

	virtual void show()
	{
		cout << name << endl;
	}
private:
	string name;
};

int main()
{
	H_child_node Level_Max("老板");
	L_child_node Level_P8("P8级高级专家,大明");
	H_child_node Level_P7("P7级高管,萨姆");

	//老板手下管理两人
	Level_Max.add(&Level_P8);
	Level_Max.add(&Level_P7);

	//高管手下管理两人
	L_child_node Level_P6("P6级码农,菜狗");
	L_child_node Level_P5("P5级码农,菜鸟");
	Level_P7.add(&Level_P6);
	Level_P7.add(&Level_P5);

	//展示公司所有员工
	Level_Max.show();

	cout << "互谅网寒冬,裁员" << endl;
	Level_P7.remove(&Level_P5);
	Level_Max.remove(&Level_P8);

	//展示公司所有员工
	Level_Max.show();

	system("pause");
	return 1;
}

组合模式解耦了客户程序与复杂元素内部结构,从而使客户程序可以像处理简单元素一样来处理复杂元素。
如果你想要创建层次结构,并可以在其中以相同的方式对待所有元素,那么组合模式就是最理想的选择

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值