Composite(组合)

8. Composite(组合)

8.1定义

  将对象组合成树形结构以表示“部分—整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性

8.2 优点

  ■ 高层模块调用简单。一棵树形机构中的所有节点都是 Component,局部和整体对调用者来说没有任何区别,即高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。

  ■ 节点自由增加。使用组合模式后,如果想增加一个树枝节点、树叶节点只需要找到其父节点即可。

8.3 缺点

  ■ 不易控制树枝构件的类型;

  ■ 不易使用继承的方法来增加新的行为。

8.4 使用场景

  ■ 需要描述对象的部分和整体的等级结构,如树形菜单、文件和文件夹管理。

  ■ 需要客户端忽略个体构件和组合构件的区别,平等对待所有的构件。组合模式也是应用广泛的一种设计模式。

8.5 c++源码实例

  1 #include<iostream>
  2 #include <list>
  3 #include <memory>
  4 #include <stdio.h>
  5 #include <string>
  6 using namespace std;
  7 
  8 //
  9 class Company {
 10 public:
 11     virtual string getInfo() = 0;
 12     virtual  string getName()
 13     {
 14         return name;
 15     }
 16     virtual string getType() = 0;
 17 protected:
 18     string name;
 19     string position;
 20     int salary;
 21 };
 22 class ConcreteCompany :public Company {
 23 public:
 24     ConcreteCompany(string name, string position, int salary) {
 25         this->name = name;
 26         this->position = position;
 27         this->salary = salary;
 28     }
 29     void add(Company* company) {
 30 
 31         shared_ptr<Company> temp(company);
 32         companyList.push_back(temp);
 33     }
 34     string getType() {
 35         return "ConcreteCompany";
 36     }
 37     void remove(string companyName) {
 38         list<shared_ptr<Company>>::iterator iter = companyList.begin();
 39             for (; iter != companyList.end(); iter++)
 40             {
 41                     if ((*iter).get()->getName() == companyName)
 42                     {
 43                         companyList.erase(iter);
 44                         return;
 45                     }
 46             }
 47     }
 48     list<shared_ptr<Company>> getChild() {
 49         return companyList;
 50     }
 51     string getInfo() {
 52         string info = "";
 53         info = "名称:" + this->name;
 54         info = info + "\t职位:" + this->position;
 55         info = info + "\t薪水:" + to_string(this->salary);
 56         return info;
 57     }
 58 private:
 59     list<shared_ptr<Company>> companyList;
 60 };
 61 
 62 class Employee :public Company {
 63 public:
 64     Employee(string name, string position, int salary) {
 65         this->name = name;
 66         this->position = position;
 67         this->salary = salary;
 68     }
 69     string getType() {
 70         return "Employee";
 71     }
 72     string getInfo() {
 73         string info = "";
 74         info = "名称:" + this->name;
 75         info = info + "\t职位:" + this->position;
 76         info = info + "\t薪水:" + to_string(this->salary);
 77         return info;
 78     }
 79 };
 80 void disPlay(ConcreteCompany* root) {
 81 
 82     cout << root->getInfo() << endl;
 83     list<shared_ptr<Company>> tmp = root->getChild();
 84     list<shared_ptr<Company>>::iterator iter = tmp.begin();
 85     for (; iter != tmp.end(); iter++)
 86     {
 87         if ((*iter).get()->getType() == string("Employee"))
 88         {
 89             cout << (*iter).get()->getInfo() << endl;
 90         }
 91         else {
 92             disPlay((ConcreteCompany*)(*iter).get());
 93         }
 94     }
 95 
 96 }
 97 int main() {
 98     ConcreteCompany* root = new ConcreteCompany("张三", "CEO", 100000);
 99     ConcreteCompany* develop = new ConcreteCompany("李四", "研发组长", 100000);
100     ConcreteCompany* sale = new ConcreteCompany("王二", "销售组长", 100000);
101     Employee *e1 = new Employee("A", "研发",200);
102     Employee *e2 = new Employee("B", "销售", 200);
103     Employee *e3 = new Employee("C", "研发", 200);
104     Employee *e4 = new Employee("D", "销售", 200);
105     root->add(develop);
106     root->add(sale);
107     develop->add(e1);
108     develop->add(e3);
109     sale->add(e2);
110     sale->add(e4);
111     disPlay(root);
112     develop->remove("A");
113     disPlay(root);
114     delete root;
115     root = NULL;
116 
117 }

 

posted @ 2020-11-05 14:32  昨日明眸  阅读(405)  评论(0)    收藏  举报