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