(本博客旨在个人总结回顾)
1、详情:
组合模式:将对象组合成树形结构以表示"部分--整体"的层次结构。composite使得客户对单个对象和复合对象使用具有一致性。
说明:
何时使用:在需求中是体现部分与整体层次的结构是,以及在我们希望用户可以忽略组合对象和单个对象的不同,统一地使用组合结构中的所有对象时,此时就可以考虑组合模式。
优点: ①高层模块调用简单。 ②节点自由增加。
缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则
2.1、UML类图:
2.2、例子源码
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
// TODO: 在此处引用程序需要的其他头文件
Component.h(单个对象和复合对象实现的接口)
#pragma once
class Component
{
public:
Component(string name);
virtual ~Component();
public:
virtual void Add(Component* pComponent) = 0;
virtual void Remove(Component* pComponent) = 0;
virtual void Display(int depth) = 0;
protected:
string m_strName;
};
Component.cpp
#include "stdafx.h"
#include "Component.h"
Component::Component(string name)
: m_strName(name)
{
}
Component::~Component()
{
}
Leaf.h
#pragma once
#include "Component.h"
class Leaf :
public Component
{
public:
Leaf(string name);
~Leaf();
public:
void Add(Component* pComponent);
void Remove(Component* pComponent);
void Display(int depth);
};
Leaf.cpp
#include "stdafx.h"
#include "Leaf.h"
Leaf::Leaf(string name):Component(name)
{
}
Leaf::~Leaf()
{
}
void Leaf::Add(Component* pComponent)
{
cout << "The Leaf don't to Add!" << endl;
}
void Leaf::Remove(Component* pComponent)
{
cout << "The Leaf do't to Remove!" << endl;
}
void Leaf::Display(int depth)
{
cout << string(depth, '-').c_str() << m_strName.c_str() << endl;
}
Composite.h
#pragma once
#include "Component.h"
#include <list>
class Composite :
public Component
{
public:
Composite(string name);
~Composite();
public:
void Add(Component* pComponent);
void Remove(Component* pComponent);
void Display(int depth);
private:
std::list<Component*> m_listChildren;
};
Composite.cpp
#include "stdafx.h"
#include "Composite.h"
Composite::Composite(string name) :Component(name)
{
}
Composite::~Composite()
{
Component* pComposite = NULL;
for (std::list<Component*>::iterator it = m_listChildren.begin(); it != m_listChildren.end(); it++)
{
pComposite = (Composite*)(*it);
if (NULL != pComposite)
{
delete pComposite;
pComposite = NULL;
}
}
m_listChildren.clear();
}
void Composite::Add(Component* pComponent)
{
m_listChildren.push_back(pComponent);
}
void Composite::Remove(Component* pComponent)
{
Component* pComposite = NULL;
for (std::list<Component*>::iterator it = m_listChildren.begin(); it != m_listChildren.end(); it++)
{
if (*it == pComponent)
{
pComposite = (Composite*)(*it);
m_listChildren.erase(it);
if (NULL != pComposite)
{
delete pComposite;
pComposite = NULL;
}
break;
}
}
}
void Composite::Display(int depth)
{
cout << string(depth, '-').c_str() << m_strName.c_str() << endl;
for (std::list<Component*>::iterator it = m_listChildren.begin(); it != m_listChildren.end(); it++)
{
(*it)->Display(depth + 2);
}
}
调用代码
CompositePatternMemo.cpp
// CompositePatternMemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Composite.h"
#include "Leaf.h"
int _tmain(int argc, _TCHAR* argv[])
{
Component* pComponent = new Composite("root");
pComponent->Add(new Leaf("Leaf 1"));
pComponent->Add(new Leaf("Leaf 2"));
Component* pComponentX = new Composite("ComponentX");
pComponentX->Add(new Leaf("LeafX 1"));
pComponentX->Add(new Leaf("LeafX 2"));
pComponent->Add(pComponentX);
Component* pComponentY = new Composite("ComponentY");
pComponentY->Add(new Leaf("LeafY 1"));
pComponentY->Add(new Leaf("LeafY 2"));
pComponent->Add(pComponentY);
Component* pComponentZ = new Composite("ComponentZ");
pComponentZ->Add(new Leaf("LeafZ 1"));
pComponentZ->Add(new Leaf("LeafZ 2"));
pComponent->Add(pComponentZ);
pComponent->Remove(pComponentZ);
pComponent->Display(1);
delete pComponent;
pComponent = NULL;
system("pause");
return 0;
}