当前位置: 代码迷 >> 综合 >> [C++]组合模式
  详细解决方案

[C++]组合模式

热度:79   发布时间:2024-02-28 05:06:23.0

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

 // 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//// 参考大话设计模式#include <iostream>
#include <vector>using namespace std;class Component {
public:Component() {}Component(string name) {name_ = name;}virtual void Add(Component* component) {};virtual void Remove(Component* component) {};virtual void Display(int depth) {};public:string name_;
};class Leaf : public Component {
public:Leaf(string name) {name_ = name;}void Add(Component* component) {cout << "not have a component" << endl;}void Remove(Component* component) {cout << "not have a component" << endl;}void Display(int depth) {for (int n = 0; n < depth; n++) {cout << "-";}cout << name_ << endl;}; 
};class Composite : public Component {
public:Composite(string name) {name_ = name;}void Add(Component* component) {component_vector_.push_back(component);}void Remove(Component* component) {component_vector_.emplace_back(component);}void Display(int depth) {for (int n = 0; n < depth; n++) {cout << "-";}cout << name_ << endl;for (auto iter = component_vector_.begin(); iter != component_vector_.end(); iter++) {(*iter)->Display(depth + 2);}}private:vector<Component*> component_vector_;
};int main()
{Composite* root = new Composite("root");root->Add(new Leaf("leaf_a"));root->Add(new Leaf("leaf_b"));Composite* comp_1 = new  Composite("comp_1");comp_1->Add(new Leaf("comp1_a"));comp_1->Add(new Leaf("comp1_b"));root->Add(comp_1);Composite* comp_2 = new  Composite("comp_2");comp_2->Add(new Leaf("comp2_a"));comp_2->Add(new Leaf("comp2_b"));comp_1->Add(comp_2);Composite* comp_3 = new  Composite("comp_3");comp_3->Add(new Leaf("comp3_a"));comp_3->Add(new Leaf("comp3_b"));root->Add(comp_3);root->Display(1);return 0;
}

 

  相关解决方案