组合模式(Composite)——结构型模式

发布于:2024-05-08 ⋅ 阅读:(24) ⋅ 点赞:(0)

组合模式(Composite)——结构型模式

组合模式是一种结构型设计模式, 你可以使用它将对象组合成树状结构, 并且能通过通用接口像独立整体对象一样使用它们。如果应用的核心模型能用树状结构表示, 在应用中使用组合模式才有价值。

例如一个场景:你有两类对象: 产品盒子 。 一个盒子中可以包含多个 产品或者几个较小的 盒子 。 这些小 盒子中同样可以包含一些 产品或更小的 盒子 , 以此类推。假设你希望在这些类的基础上开发一个定购系统。 订单中可以包含无包装的简单产品, 也可以包含装满产品的盒子…… 以及其他盒子。 此时你会如何计算每张订单的总价格呢?

在这里插入图片描述

组合模式建议使用一个通用接口来与 产品盒子进行交互, 并且在该接口中声明一个计算总价的方法。

那么方法该如何设计呢?

对于一个产品, 该方法直接返回其价格;

对于一个盒子, 该方法遍历盒子中的所有项目, 询问每个项目的价格, 然后返回该盒子的总价格。

如果其中某个项目是小一号的盒子, 那么当前盒子也会遍历其中的所有项目, 以此类推, 直到计算出所有内部组成部分的价格。 你甚至可以在盒子的最终价格中增加额外费用, 作为该盒子的包装费用。

该方式的最大优点在于你无需了解构成树状结构的对象的具体类。 你也无需了解对象是简单的产品还是复杂的盒子。 你只需调用通用接口以相同的方式对其进行处理即可。 当你调用该方法后, 对象会将请求沿着树结构传递下去。


用C++实现一个组合图形的例子,可以把compoundGraphic_im1理解成主窗口,主窗口中有自己的图形rectangle和一个子窗口。子窗口(compoundGraphic_im2)中有两个rectangle和一个circle。现在要一键绘制或一键移动,整合成一个整体,可以用组合模式实现。

在这里插入图片描述

#include <iostream>
#include <vector>
#include <memory>

using namespace std;

class Graphic {
public:
    virtual void move(int, int) = 0;
    virtual void draw() = 0;
};

class Rectangle : public Graphic {
public:
    void move(int x, int y) override {
        cout << "rectangle move" << x << ", " << y << endl;
        return ;
    }
    void draw() override {
        cout << "draw rectangle" << endl;
        return ;
    }
};

class Circle : public Graphic {
public:
    void move(int x, int y) override {
        cout << "Circle move" << x << ", " << y << endl;
        return ;
    }
    void draw() override {
        cout << "draw Circle" << endl;
        return ;
    }
};

class CompoundGraphic : public Graphic {
private:
    vector<Graphic *> child;

public:
    void add(Graphic *child) {
        this->child.push_back(child);
        return ;
    }

    void remove(const Graphic* child) {
        /* ... */
    }

    void move(int x, int y) override {
        for (auto& child : child) {
            child->move(x, y);
        }
    }

    void draw() override {
        for (auto& child : child) {
            child->draw();
        }
    }
};

void ClientCode() {
    CompoundGraphic *compoundGraphic_im2 = new CompoundGraphic();
    compoundGraphic_im2->add(new Rectangle());
    compoundGraphic_im2->add(new Circle());
    compoundGraphic_im2->add(new Rectangle());

    CompoundGraphic *compoundGraphic_im1 = new CompoundGraphic();
    compoundGraphic_im1->add(compoundGraphic_im2);
    compoundGraphic_im1->add(new Rectangle());

    compoundGraphic_im1->move(3, 4);   // all
    compoundGraphic_im1->draw();   // all

    return ;
}

网站公告

今日签到

点亮在社区的每一天
去签到