Charpter19 combined mode

Combined mode Introduction

Combined mode (Composite) , combining objects in a tree structure to represent 'part - whole' hierarchy. Mode allows for the use of a combination of individual objects and combinations of objects uniformly.

When the discovery demand is reflected in part the whole time hierarchical structure, and you want the user can ignore the different combinations of objects with a single object, when unified access to all objects in the composite structure, you should consider using a combination mode.

The benefits of the combination model is the basic objects can be combined into more complex objects, and objects have this combination can be combined, so keep recursion, client code, any place used basic objects can use a combination of objects. Users do not care about in the end is a leaf node is processing or handling a component, you will not have to define a combination of written statements to determine the number of options. Combination mode enables customers to consistently use a combination of structural and individual objects.

UML class diagram combination pattern

 

 The method used to manage all the declared objects in Component subclasses, including Add, Remove the like. All subclasses achieved Component interfaces are equipped with the Add and Remove. The benefit of this is the leaf node and the branch node is no different for the outside world, they have exactly the same behavior interface. But the problem is also evident, because Leaf class itself does not have the Add (), Remove () function method, so it makes no sense to realize. This method is called in a transparent manner.

If not declared Add and Remove methods in the Component interface, then a subclass of the Leaf will not need to implement it, but in the Composite declare all methods to manage subclass object, but due to the lack of transparency, so the leaves and branches class will not have the same interface, call the client needs to make appropriate judgments, the inconvenience.

C ++ code to achieve

// 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;
}

operation result:

 

Guess you like

Origin www.cnblogs.com/yb-blogs/p/12589716.html