C++设计模式之组合模式(九)

composite pattern

简介

  组合模式(Composite Pattern)是将多个对象组合形成树形结构,来表示具有“整体-部分”关系的层次结构。组合模式对单个对象(即:叶子构件)和组合对象(即:容器构件)的使用具有一致性,组合模式又被称为“整体-部分”(Part-Whole)模式,属于结构型模式。
组合模式UML图

  • Component: 定义一个接口。
  • Leaf: 为叶子构件,不包含组件构件。
  • Composite: 为组合构件,包含子组件构件。

代码

// composite_pattern.h
class Component {
public:
    virtual ~Component() = default;

    virtual void Operator();

    virtual void add(std::shared_ptr<Component> component);

    virtual void remove(int index);

    virtual std::shared_ptr<Component> get_current_component(int index);
}; // component

class Leaf : public Component {
public:
    Leaf(std::string name);
    virtual void Operator();

private:
    std::string name_;
}; // Leaf

class Composite : public Component {
public:
    Composite(std::string name);
    virtual void Operator();

    void add(std::shared_ptr<Component> component);

    void remove(int index);

    std::shared_ptr<Component> get_current_component(int index);

private:
    std::vector<std::shared_ptr<Component> > com_v_;

    std::string name_;
}; // Composite
// composite_pattern.cpp
void Component::Operator() {

}

void Component::add(std::shared_ptr<Component> component) {

}

void Component::remove(int index) {

}

std::shared_ptr<Component> Component::get_current_component(int index) {
    return nullptr;
}

Leaf::Leaf(std::string name)
    /*: name_(name)*/{
    this->name_ = name;

}

void Leaf::Operator() {
    std::cout << this->name_ << ": " << __PRETTY_FUNCTION__ << std::endl;
}

Composite::Composite(std::string name)
    /*: name_(name)*/{
    this->name_ = name;
}

void Composite::Operator() {
    //todo
    std::cout << this->name_ << ": " << __PRETTY_FUNCTION__ << std::endl;
    for (auto it : this->com_v_)
        it->Operator();
}

void Composite::add(std::shared_ptr<Component> component) {
    this->com_v_.push_back(component);
}

void Composite::remove(int index) {
    this->com_v_.erase(this->com_v_.begin() + index);
}

std::shared_ptr<Component> Composite::get_current_component(int index) {
    return this->com_v_.at(index);
}

} // compposite_pattern


int main(){
    std::shared_ptr<Component> root = std::make_shared<Composite>("root");
    std::shared_ptr<Component> leaf_1 = std::make_shared<Leaf>("Leaf1");
    std::shared_ptr<Component> leaf_2 = std::make_shared<Leaf>("Leaf2");
    std::shared_ptr<Component> composite_1 = std::make_shared<Composite>("Composite1");
    std::shared_ptr<Component> composite_2 = std::make_shared<Composite>("Composite2");

    composite_1->add(composite_2);
    composite_1->add(leaf_2);

    root->add(leaf_1);
    root->add(leaf_2);
    root->add(composite_1);
    root->add(composite_2);

    root->Operator();
    std::cout << "*****************************************" << std::endl;
    std::shared_ptr<Component> component_tmp =  root->get_current_component(2);
    component_tmp->Operator();
	return 0;
}

// output
/*
root: virtual void patterns::compposite_pattern::Composite::Operator()
Leaf1: virtual void patterns::compposite_pattern::Leaf::Operator()
Leaf2: virtual void patterns::compposite_pattern::Leaf::Operator()
Composite1: virtual void patterns::compposite_pattern::Composite::Operator()
Composite2: virtual void patterns::compposite_pattern::Composite::Operator()
Leaf2: virtual void patterns::compposite_pattern::Leaf::Operator()
Composite2: virtual void patterns::compposite_pattern::Composite::Operator()
*****************************************
Composite1: virtual void patterns::compposite_pattern::Composite::Operator()
Composite2: virtual void patterns::compposite_pattern::Composite::Operator()
Leaf2: virtual void patterns::compposite_pattern::Leaf::Operator()
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值