专注于技术 心无旁骛 - justforfan528

Linux技术| 网络编程coding | 网络安全Cyber Security | study |

 

导航

设计模式简单代码之Composite模式

/**********************************************************************************************************\
* 作者:一雨田(
http://blog.csdn.net/dylgsy/)。本文可随便转贴,但请保留此信息
*
* Composite模式说明:
*
* 将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性.
*
* Composite比较容易理解,想到Composite就应该想到树形结构图。组合体内这些对象都有共同接口,当组合体一个对象的
* 方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。
* 可以用牵一动百来形容。
*
* 所以Composite模式使用到Iterator模式,和Chain of Responsibility模式类似。
*
* Composite是个很巧妙体现智慧的模式,在实际应用中,如果碰到树形结构,我们就可以尝试是否可以使用这个模式。
\*********************************************************************************************************
*/

/**********************************************************************************************************\
* 任务描述:
* 学校里的一个班中,老师和学生的关系可以使用Composite模式模拟
* 老师让学生去打扫卫生!对老师来说,让一个学生去打扫卫生和让一个组的学生去打扫卫生是一样的,
* 他可不用去到小组里一个一个的去通知学生们。
\*********************************************************************************************************
*/

#include <iostream>
#include 
<vector>
using namespace std;

// 定义人的抽象类,相当于Composite模式的Component类
class CPeopleComponent
{
public:
 CPeopleComponent()
 {
 }
 
virtual ~CPeopleComponent()
 {
 }
 
 
virtual void DoCleaning() = 0;

 
virtual bool Add(const CPeopleComponent& people)
 {
  
return false;
 }

 
virtual bool Remove(CPeopleComponent people)
 {
  
return true;
 }
 
 
virtual CPeopleComponent* GetChild(int nIndex)
 {
  
return 0;
 }

protected:
 
char szName[20];
};

// 定义学生组合类,相当于Composite模式的Composite类
// 这里的vector借用一下STL的vector
// 正如GOF《设计模式》所说的,如果语言没有实现继承,那
// 继承也是我们要实现的模式。
class CStudentComposite: public CPeopleComponent
{
public:
 
 
virtual void DoCleaning()
 {
  vector
<CPeopleComponent*>::iterator pcIter = pcVec.begin();
  
for(; pcIter != pcVec.end(); pcIter++)
  {
   (
*pcIter)->DoCleaning();
  }
 }

 
virtual bool Add(CPeopleComponent* people)
 {
  pcVec.push_back(people);
  
return true;
 }

 
virtual bool Remove(CPeopleComponent* people)
 {
  pcVec.erase(
&people);
  
return true;
 }
 
 
virtual CPeopleComponent* GetChild(int nIndex)
 {
  
return pcVec[nIndex];
 }

 
// 提供一个函数清除资源
 virtual void ReleaseAll()
 {
  vector
<CPeopleComponent*>::iterator pcIter = pcVec.begin();
  
for(; pcIter != pcVec.end(); pcIter++)
  {
   delete (
*pcIter);
  }
 }

private:
 vector
<CPeopleComponent*> pcVec;
};

class CStudent: public CPeopleComponent
{
public:
 CStudent(
const char *pszName)
 {
  strcpy(szName, pszName);
 }
 
virtual ~CStudent()
 {
 } 
 
virtual void DoCleaning()
 {
  cout 
<< "我是学生:" << szName;
  cout 
<< " 我打扫卫生去咯" << endl;
 } 
};


// 好,我们来模拟这个场景
void main()
{
 
// 1个学生,老师是这样叫他的
 CStudent *= new CStudent("1号");
 s
->DoCleaning();

 
// 1组学生,老师是这样叫他们的
 char szName[20];
 CStudentComposite sGroup;
 
for(int i = 2; i < 11; i++)
 {
  sprintf(szName, 
"%d号", i);
  CStudent 
*ss = new CStudent(szName);
  sGroup.Add(ss);
 }

 sGroup.DoCleaning();

 
// 可以看出老师对待一个或者一组学生的对待方式是一样的
 
// 这个就是Composite模式的精神所在了
 
 delete s;
 sGroup.ReleaseAll();
}

/**********************************************************************************************************************\
* 最后要说的是:
* Composite模式在实现中有一个问题就是要提供对于子节点(Leaf)的管理策略,这里使用的是STL 中的vector,可以提供其他的
* 实现方式,如数组、链表、Hash表等。
* 当然这里只是入门,要真正理解和用好Composite模式,还需要大家积极努力去探索
\*********************************************************************************************************************
*/


posted on 2007-10-16 08:44  游荡者  阅读(536)  评论(0编辑  收藏  举报