Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1064577
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2014-11-14 15:57:19

     


点击(此处)折叠或打开

  1. class file
  2. {
  3. public:
  4.     file()
  5.     {

  6.     }
  7.     ~file()
  8.     {

  9.     }
  10.     virtual void show()
  11.     {
  12.         std::list<file*>::iterator itr = m_Treelist.begin();
  13.         for (; itr != m_Treelist.end(); itr++)
  14.         {
  15.             (*itr)->show();
  16.         }
  17.     }
  18.     virtual void insert(file* tmp)
  19.     {
  20.         m_Treelist.push_back(tmp);
  21.     }
  22. private:
  23.     std::list<file*> m_Treelist;
  24. };

  25. class realFile : public file{
  26. public:
  27.     realFile() :file()
  28.     {

  29.     }
  30.     ~realFile()
  31.     {

  32.     }

  33.     void show()
  34.     {
  35.         printf("I am a file \n");
  36.     }
  37. };

  38. class folder : public file
  39. {
  40. public:
  41.     folder() :file()
  42.     {

  43.     }
  44.     ~folder()
  45.     {

  46.     }
  47.     
  48. };

点击(此处)折叠或打开

  1. file *root = new folder();
  2.     file *file1 = new realFile();
  3.     file *highmov = new folder();
  4.     root->insert(file1);
  5.     file *highfile = new realFile();
  6.     highmov->insert(highfile);
  7.     root->insert(highmov);
  8.     root->show();


组合模式解耦了客户程序与复杂元素内部结构,从而使客户程序可以向处理简单元素一样来处理复杂元素。

如果你想要创建层次结构,并可以在其中以相同的方式对待所有元素,那么组合模式就是最理想的选择。

阅读(1694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~