设计模式——组合模式(C++)

/*****************************************
Copyright (c) 2016 Jingshuang Hu

@filename:Component.h
@datetime:2016.09.19
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#ifndef _COMPONENT_H
#define _COMPONENT_H

#include 
#include 

// 定义接口
class Component {
public:
    virtual void operation() = 0;
    virtual void add(Component* component) = 0;
    virtual void remove(Component* component) = 0;
    virtual Component* getChild(int index) = 0;
};

// 叶节点,继承并实现了operation方法
// 也继承了add、remove、getChild等操作,但叶节点没有孩子,因此这些方法对叶节点无意义。
class Leaf : public Component {
private:
    std::string name;
public:
    Leaf(std::string name);
    virtual void operation();
    virtual void add(Component* component);
    virtual void remove(Component* component);
    virtual Component* getChild(int index);
};

// 定义组件,继承并实现了基类中的所有方法,组件可以添加叶节点或新的组件
class Composite : public Component {
private:
    std::string name;
    std::list *lst;
public:
    Composite(std::string name);
    virtual void operation();
    virtual void add(Component* component);
    virtual void remove(Component* component);
    virtual Component* getChild(int index);
};

#endif // _COMPONETN_H
/*****************************************
Copyright (c) 2016 Jingshuang Hu

@filename:Component.cpp
@datetime:2016.09.19
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#include "Component.h"

#include 
#include 
#include 

using namespace std;

// 抽象接口
void Component::operation() {}

void Component::add(Component* component) {}

void Component::remove(Component* component) {}

Component* Component::getChild(int index) { 
    return NULL;
}

// 叶节点
Leaf::Leaf(string name) {
    this->name = name;
}

void Leaf::operation() {
    cout << "Leaf:" + name + ":operation" << endl;
}

void Leaf::add(Component* component) {}

void Leaf::remove(Component* component) {}

Component* Leaf::getChild(int index) {
    return NULL;
}

// 组件
Composite::Composite(string name) {
    this->name = name;
    lst = new list;
}

void Composite::operation() {
    cout << "Composite:" + name + ":operation" << endl;
    list::iterator it = lst->begin();
    for (; it != lst->end(); it++) {
        (*it)->operation();
    }
}

void Composite::add(Component* component) {         // 添加叶节点或组件
    lst->push_back(component);
}

void Composite::remove(Component* component) {      // 移除叶节点或组件
    lst->remove(component);
}

Component* Composite::getChild(int index) {
    if (index < 0 || index >= lst->size()) {
        return NULL;
    }
    list::iterator it = lst->begin();
    for (int i = 0; i < index; i++){
        it++;
    }
    return *it;
}
/*****************************************
Copyright (c) 2016 Jingshuang Hu

@filename:main.cpp
@datetime:2016.09.19
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#include 
#include "Component.h"

using namespace std;

// 简易层次结构
// root +---->leafA 
//      +----->comA  +----->leafB1
//                   +----->leafB2
//                   +----->comB   +----->leafC
//                                 +----->comC  +----->leafD

// 以UI设计的角度来看,非常形象,很好理解。
int main() {
    Component* root = new Composite("root");        // 可看做Panel

    Leaf* leafA = new Leaf("A");                    // 可看做控件
    Leaf* leafB1 = new Leaf("B1");
    Leaf* leafB2 = new Leaf("B2");
    Leaf* leafC = new Leaf("C");
    Leaf* leafD = new Leaf("D");

    Composite* comA = new Composite("A");           // 可看做subPanel
    Composite* comB = new Composite("B");
    Composite* comC = new Composite("C");

    root->add(leafA);
    root->add(comA);
    comA->add(leafB1);
    comA->add(leafB2);
    comA->add(comB);
    comB->add(leafC);
    comB->add(comC);
    comC->add(leafD);

    root->operation();

    comA->remove(comB);
    root->operation();

    Component* com = comA->getChild(1);
    if (com) {
        com->operation();
    }

    system("pause");
    return 0;
}
// 组合模式:允许将对象组合成树形结构来表现“整体-部分”层次结构。组合能让客户以一致的方式处理个别对象以及组合。
设计模式——组合模式(C++)_第1张图片

你可能感兴趣的:(设计模式)