DesignPatterns_Composite

本文探讨了复合模式在面向对象编程中的应用,通过组件和叶节点的层次结构实现部分与整体的关系,允许客户端以统一的方式处理单个对象和复合对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

////////////////////////////////////////////////////////////////////////
// Composite
// - Compose objects into tree structures to represent part-whole 
//   hierarchies. Composite lets client treat individual objects and 
//   compositons of objects uniformly.
//
// Author     : ZAsia
// Date       : 15/05/09
// Warning    : In practice, declaration and implementation should
//              be separated(.h and .cpp).
////////////////////////////////////////////////////////////////////////
#include 
#include 
using namespace std;

// Component
// - declares the interface for objects in the composition
// - implements default behavior for the interface common to all 
//   classes, as appropriate.
// - declares an interface for accessing and managing its child components.
// - (optional) defines an interface for accessing a component's parent in
//   the recursive structure, and implements it if that's appropriate.
class Component
{
public:
	Component(const char *name) : _name(name) { }
	// The destructor needs to be virtual to allow objects
	// in the heritance hierarchy to be dynamically allocated.
	virtual ~Component() { }
	const char *Name() { return _name; }
	virtual void Operation() = 0;
	virtual void Add(Component *com) { }
	virtual void Remove(Component *com) { }
	virtual Component *GetChild(unsigned int) { return nullptr; }
private:
	const char *_name;
};

// Leaf
// - represents leaf objects in the composition. A leaf has no children.
// - defines behavior for primitive objects in the compositon.
class Leaf : public Component
{
public:
	Leaf(const char *name) : Component(name) { }
	~Leaf() { cout << "delet " << Name() << endl; }
	virtual void Operation() { cout << "I'm " << Name() << endl; }
};

// Composite
// - defines behavior for components haveing children
// - stores child components
// - implements chile-related operations in the Component interface
class Composite : public Component 
{
public:
	Composite(const char *name) : Component(name) { }

	~Composite()
	{
		cout << "delet " << Name() << endl;
		for (vector::iterator iter = vecComponent.begin();
			iter != vecComponent.end(); iter = vecComponent.begin())
		{
			delete *iter;
			vecComponent.erase(iter);	
		}
	}

	virtual void Operation()
	{
		cout << "I'm " << Name() << " , now children introduce yourself:" << endl;
		for (vector::iterator iter = vecComponent.begin();
			iter != vecComponent.end(); ++iter)
		{
			(*iter)->Operation();
		}
	}

	virtual void Add(Component *com)
	{
		vecComponent.push_back(com);
	}

	virtual void Remove(Component *com)
	{
		for (vector::iterator iter = vecComponent.begin();
			iter != vecComponent.end(); ++iter)
		{
			if (strcmp((*iter)->Name(), com->Name()) == 0)
			{
				delete *iter;
				vecComponent.erase(iter);
				break;
			}
		}
	}

	virtual Component *GetChild(unsigned int index)
	{
		if (index >= vecComponent.size())
		{
			return nullptr;
		}
		return vecComponent[index];
	}

private:
	vector vecComponent;
};

// Client
// - manipulates objects in the composition through the Component interface
//
// Collaborations
// - Clients use the Component class interface to interact with objects in the
//  composite structure. If the recipient is a Leaf, then the request is handled
//  directly. If the recipient is a Composite, then it usually forwards requests
//  to its child components, possibly performing additional operations before
//  and/or after forwarding.
int main()
{
	// Composite
	Component *pComposite = new Composite("Suzhou Head Office");
	// Add ChildLeaf
	pComposite->Add(new Leaf("Suzhou Human Resource Department"));
	pComposite->Add(new Leaf("Suzhou Research and Development Department"));
	// Add ChildComposite
	pComposite->Add(new Composite("Beijing Branch"));
	// Add Leaf to ChildComposite
	pComposite->GetChild(2)->Add(new Leaf("Beijing Human Resource Department"));
	pComposite->GetChild(2)->Add(new Leaf("Beijing Research and Development Department"));

	// Operation
	pComposite->Operation();
	pComposite->GetChild(2)->Operation();

	// Close Beijing Branch
	pComposite->Remove(pComposite->GetChild(2));
	
	// Close Suzhou Head Office
	if (pComposite != nullptr)
	{
		delete pComposite;
		pComposite = nullptr;
	}

	return 0;
}

////////////////////////////////////////////////////////////////////////////
// 1. defines class hierarchies consisting of primitive objects and composite
//   objects. Primitive objects can be composed into more complex objects, 
//   which in turn can be composed, and so on recursively. Wherever client
//   code expects primitive object, it can also take a composite object.
// 2. Who should delete components? In language without garbage collection, 
//    it's usually best to make a Composite responsible for deleting its
//    children when it's destroyed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值