9、C++设计模式——组合模式(菜鸟教程例子)

原例网址:组合模式(菜鸟教程)

组合模式

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。

我们通过下面的实例来演示组合模式的用法。实例演示了一个组织中员工的层次结构。

 

复制代码
  1 #include "iostream"
  2 #include "string"
  3 #include "list"
  4 using namespace std;
  5 
  6 //c++11标准增加了全局函数std::to_string,VS2010并没有支持到C++11的这部分
  7 //自定义string to_String(int)函数
  8 #define max 100
  9 string my_to_string(int n)
 10 {
 11     int m = n;
 12     char s[max];
 13     char ss[max];
 14     int i=0,j=0;
 15     if (n < 0)// 处理负数
 16     {
 17         m = 0 - m;
 18         j = 1;
 19         ss[0] = '-';
 20     }
 21     while (m>0)
 22     {
 23         s[i++] = m % 10 + '0';
 24         m /= 10;
 25     }
 26     s[i] = '\0';
 27     i = i - 1;
 28     while (i >= 0)
 29     {
 30         ss[j++] = s[i--];
 31     }
 32     ss[j] = '\0';
 33     return ss;
 34 }
 35 
 36 
 37 //步骤1 创建 Employee 类,该类带有 Employee 对象的列表
 38 class Employee
 39 {
 40 public:
 41     //构造函数
 42     Employee(string name, string dept, int sal)    
 43     {
 44         this->name = name;
 45         this->dept = dept;
 46         this->salary = sal;
 47         subordinates = new list<Employee*>;
 48     }
 49 
 50     void add( Employee* e )
 51     {
 52         subordinates->push_back(e);
 53     }
 54 
 55     void remove( Employee* e )
 56     {
 57         subordinates->remove(e);
 58     }
 59 
 60     list<Employee*>* getSubordinates()
 61     {
 62         return subordinates;
 63     }
 64 
 65     string toString()
 66     {
 67         return ("Employee :[ Name : "+ name 
 68             +", dept : "+ dept + ", salary :"
 69             + my_to_string(salary) +" ]");
 70     }
 71 
 72 private:
 73     string name;
 74     string dept;
 75     int salary;
 76     list<Employee*>* subordinates;
 77 };
 78 
 79 
 80 
 81 //步骤2 使用 Employee 类来创建和打印员工的层次结构
 82 void main()
 83 {
 84     Employee* CEO = new Employee("John", "CEO", 30000);
 85     
 86     Employee* headSales = new Employee("Robert", "Head Sales", 20000);
 87     
 88     Employee* headMarketing = new Employee("Michel", "Head Marketing", 20000);
 89 
 90     Employee* clerk1 = new Employee("Laura", "Marketing", 10000);
 91     Employee* clerk2 = new Employee("Bob", "Marketing", 10000);
 92 
 93     Employee* salesExecutive1 = new Employee("Richard", "Sales", 10000);
 94     Employee* salesExecutive2 = new Employee("Rob", "Sales", 10000);
 95 
 96     CEO->add(headSales);
 97     CEO->add(headMarketing);
 98 
 99     headSales->add(salesExecutive1);
100     headSales->add(salesExecutive2);
101 
102     headMarketing->add(clerk1);
103     headMarketing->add(clerk2);
104 
105     cout << CEO->toString() << endl;
106     list<Employee*>::iterator itera1;
107     list<Employee*>::iterator itera2;
108     for ( itera1 = CEO->getSubordinates()->begin();
109         itera1 != CEO->getSubordinates()->end();
110         itera1++ )
111     {
112         cout << (*itera1)->toString() << endl;
113         for ( itera2 = (*itera1)->getSubordinates()->begin();
114             itera2 != (*itera1)->getSubordinates()->end();
115             itera2++ )
116         {
117             cout << (*itera2)->toString() << endl;
118         }
119     }
120 
121     delete CEO;
122     delete headSales;
123     delete headMarketing;
124     delete clerk1;
125     delete clerk2;
126     delete salesExecutive1;
127     delete salesExecutive2;
128 
129     system("pause");
130 }
复制代码

 

运行结果:

posted @   张三Nefe  阅读(328)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示