设计模式笔记之---组合模式

组合模式(Composite):将对象组合成树状结构以表示“整体-部分”层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。

UML类图:

               

模式说明:

Component
       是组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component子部件。
Leaf
        在组合中表示叶子结点对象,叶子结点没有子结点。
Composite 
        定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关操作,如增加(add)和删除(remove)等。
以下情况下适用Composite模式:
1.你想表示对象的部分-整体层次结构
2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

基本代码:

/********************************************************************
filename: 	Composite.h
created:	2013-01-24
author:		firehood

purpose:	firehood 学设计模式之---组合模式
*********************************************************************/
#pragma once
#include <iostream> 
#include <string>
#include <list>
using namespace std;  

// 组合对象(树状结构)
class Component
{
public:
	Component(string name):m_strName(name){}
	virtual ~Component(void){}
public:
	virtual void add(Component* pChild){}
	virtual void remove(Component* pChild){}
	virtual Component* getChild(int index){return NULL;}
	virtual void operation(int depth) = 0;
protected:
	string  m_strName;
};

// 叶子节点
class Leaf : public Component
{
public:
	Leaf(string name):Component(name){}
	virtual ~Leaf(void){}
public:
	virtual void operation(int depth)
	{
		for(int i=0;i<depth;i++)
		{
			cout<<"--";
		}
		cout<<m_strName<<endl;
	}
};

// 枝节点
class Composite : public Component
{
public:
	Composite(string name):Component(name){m_ComponentList.clear();}
	virtual ~Composite(void)
	{
		for(list<Component*>::iterator iter = m_ComponentList.begin(); iter != m_ComponentList.end(); iter++)  
		{  
			delete (*iter);
		} 
	}
public:
	virtual void add(Component *pChild)
	{
		if(pChild != NULL)
		{
			m_ComponentList.push_back(pChild);
		}
	}
    virtual void remove(Component* pChild)
	{
		if(pChild == NULL)  
		{  
			m_ComponentList.remove(pChild);
		}  
	}
	virtual Component* getChild(int index)
	{
		list<Component*>::iterator iter;  
		int i = 0;
		for(iter = m_ComponentList.begin(); iter != m_ComponentList.end(); iter++)  
		{  
			if(index == i++)
			{
				return (*iter);
			}
		} 
		return NULL;
	}
	virtual void operation(int depth)
	{
		for(int i=0;i<depth;i++)
		{
			cout<<"--";
		}
		cout<<m_strName<<endl;
		list<Component*>::iterator iter;  
		for(iter = m_ComponentList.begin(); iter != m_ComponentList.end(); iter++)  
		{  
			(*iter)->operation(depth+1);
		} 
	}
private:
	list<Component*> m_ComponentList;
};

客户端调用代码

#include "Composite.h"
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
	cout<<"*************************************"<<endl;
	cout<<"firehood 学设计模式之---组合模式"<<endl;
	cout<<"*************************************"<<endl;
    
    Composite root("root");
	root.add(new Leaf("Leaf A"));
	root.add(new Leaf("Leaf B"));

	Composite branch("branch A");
	branch.add(new Leaf("Leaf X"));
    branch.add(new Leaf("Leaf Y"));

	root.add(&branch);

    Composite branch2("branch B");
	branch2.add(new Leaf("Leaf X"));
	branch2.add(new Leaf("Leaf Y"));
	branch.add(&branch2);

	root.add(new Leaf("Leaf C"));

	root.operation(1);

    system("pause");
	return 0;
}

执行结果:

*************************************
firehood 学设计模式之---组合模式
*************************************
--root
----Leaf A
----Leaf B
----branch A
------Leaf X
------Leaf Y
------branch B
--------Leaf X
--------Leaf Y
----Leaf C
请按任意键继续. . .


猜你喜欢

转载自blog.csdn.net/feihu521a/article/details/8539156