c++设计模式
组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得客户端对单个对象和组合对象的使用具有一直性。
既然讲到以树形结构表示“部分-整体”,那可以将组合模式想象成一根大树,将大树分成树枝和树叶两部分,树枝上可以再长树枝,也可以长树叶,树叶上则不能再长出别的东西。
以下情况可以考虑使用组合模式:
- 希望表示对象的部分-整体层次结构。
- 希望客户端忽略组合对象与单个对象的不同,客户端将统一的使用组合结构中的所有对象。
1,运行效果
2,代码示意
#include <iostream>
#include <vector>
#include <memory>
#include <typeinfo>
// an abstract class,privade the same interface of comnination and single object
class Item {
public:
Item(const std::string &name) : m_name(name) {};
virtual ~Item() {};
virtual void add(std::shared_ptr<Item> item) = 0;
virtual void remove(const std::string &name) = 0;
virtual void desplay(int depth) = 0;
friend std::ostream &operator<<(std::ostream &os, const Item &item);
protected:
std::string m_name;
};
std::ostream &operator<<(std::ostream &os, const Item &item) {
os << item.m_name;
return os;
}
class Branch : public Item {
public:
using Item::Item;
void add(std::shared_ptr<Item> item) override {
m_item_list.push_back(item);
};
void remove(const std::string &name) override {
std::vector<std::shared_ptr<Item>>::iterator iter;
for(iter=m_item_list.begin(); iter!=m_item_list.end(); iter++) {
m_item_list.erase(iter); return;
}
};
void desplay(int depth) override {
for(int i=0; i<depth; i++) {std::cout<< "-";}
std::cout << (*this) << std::endl;
for(auto iter : m_item_list) {
iter->desplay(depth+1);
}
};
private:
std::vector<std::shared_ptr<Item>> m_item_list;
};
class Leaf : public Item {
public:
using Item::Item;
void add(std::shared_ptr<Item> item) override {};
void remove(const std::string &name) override {};
void desplay(int depth) override {
for(int i=0; i<depth; i++) {std::cout<< "-";}
std::cout << (*this) << std::endl;
};
};
class Flower : public Item {
public:
using Item::Item;
void add(std::shared_ptr<Item> item) override {};
void remove(const std::string &name) override {};
void desplay(int depth) override {
for(int i=0; i<depth; i++) {std::cout<< "-";}
std::cout << (*this) << std::endl;
};
};
int main() {
Branch tree("tree_A");
std::shared_ptr<Branch> branch_1 = std::make_shared<Branch>("branch_1");
std::shared_ptr<Leaf> leaf_1 = std::make_shared<Leaf>("leaf_1");
std::shared_ptr<Flower> flower_1 = std::make_shared<Flower>("flower_1");
std::shared_ptr<Leaf> leaf_2 = std::make_shared<Leaf>("leaf_2");
branch_1->add(leaf_1);
branch_1->add(flower_1);
branch_1->add(leaf_2);
std::shared_ptr<Branch> branch_2 = std::make_shared<Branch>("branch_2");
std::shared_ptr<Flower> flower_2 = std::make_shared<Flower>("flower_2");
std::shared_ptr<Leaf> leaf_3 = std::make_shared<Leaf>("leaf_3");
std::shared_ptr<Flower> flower_3 = std::make_shared<Flower>("flower_3");
branch_2->add(flower_2);
branch_2->add(leaf_3);
branch_2->add(flower_3);
std::shared_ptr<Branch> branch_3 = std::make_shared<Branch>("branch_3");
std::shared_ptr<Leaf> leaf_4 = std::make_shared<Leaf>("leaf_4");
std::shared_ptr<Flower> flower_4 = std::make_shared<Flower>("flower_4");
branch_3->add(branch_2);
branch_3->add(leaf_4);
branch_3->add(flower_4);
std::shared_ptr<Branch> branch_4 = std::make_shared<Branch>("branch_4");
branch_1->add(branch_4);
tree.add(branch_1);
tree.add(branch_3);
tree.desplay(1);
return 0;
}