组合模式简介
组合模式(Composite),将对象组合成树形结构以表示‘‘部分-整体’的层次结构。组合模式使得用户对于单个对象和组合对象的使用具有一致性。
当发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。
组合模式的好处是,基本对象可以被组合成更复杂的对象,而这个组合对象有可以被组合,这样不断地递归下去,客户代码中,任何用到基本对象的地方都可以使用组合对象了。用户是不用关心到底是处理一个叶节点还是处理一个组件,也就不用为了定义组合而写一些选择判断语句了。组合模式让客户可以一致地使用组合结构和单个对象。
组合模式UML类图
在Component中声明所有用来管理子类对象的方法,其中包括Add、Remove等。这样实现Component接口的所有子类都具备了Add和Remove。这样做的好处就是叶结点和枝节点对于外界没有区别,它们具备完全一致的行为接口。但问题也很明显,因为Leaf类本身不具备Add()、Remove()方法的功能,所以实现它是没有意义的。这种方式叫做透明方式。
如果在Component接口中不去声明Add和Remove方法,那么子类的Leaf也就不需要去实现它,而是在Composite声明所有用来管理子类对象的方法,不过由于不够透明,所以树叶和树枝类将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便。
C++代码实现
// Component类 #ifndef _COMPONENT_HPP #define _COMPONENT_HPP #include<string> using namespace std; class Component{ public: Component(string name):name(name){ } virtual void add(Component* c) { }; virtual void remove(Component* c) { }; virtual void display(int n) {}; protected: string name; }; #endif
// Leaf类 #ifndef _LEAF_HPP #define _LEAF_HPP #include<iostream> #include<string> #include"component.hpp" using namespace std; class Leaf : public Component{ public: Leaf(string name) : Component(name) { } virtual void add(Component* c){ cout << "cant't add() in Leaf" << endl; } virtual void remove(Component* c) { cout << "can't remove() in Leaf" << endl; } virtual void display(int n){ for(int i = 0; i < n; ++i){ cout << '-'; } cout << name << endl; } }; #endif
// Composite类 #ifndef _COMPOSITE_HPP #define _COMPOSITE_HPP #include<iostream> #include<string> #include<list> #include"component.hpp" using namespace std; class Composite:public Component{ public: Composite(string name):Component(name){ } virtual void add(Component*c) override { l.push_back(c); } virtual void remove(Component*c) override { l.pop_back(); } virtual void display(int n) override { for(int i = 0; i < n; ++i){ cout << '-'; } cout << name <<endl; for(auto i : l) { i->display( n+4 ); } } private: list<Component*> l; }; #endif
// 客户端程序 #include<iostream> #include"composite.hpp" #include"leaf.hpp" using namespace std; int main(){ Composite* root = new Composite("root"); root->add(new Leaf("LeafA")); root->add(new Leaf("LeafB")); Composite* comp = new Composite("CompositeX"); comp->add(new Leaf("XA")); comp->add(new Leaf("XB")); root->add(comp); Composite* comp2 = new Composite("CompositeXX"); comp2->add(new Leaf("XXA")); comp2->add(new Leaf("XXB")); comp->add(comp2); root -> add(new Leaf("LeafC")); root -> add(new Leaf("LeafC")); root->display(1); getchar(); return 0; }
运行结果: