从C++示例理解开闭原则

开闭原则要求我们在编写代码时,尽量不去修改原先的代码,当出现新的业务需求时,应该通过增加新代码的形式扩展业务而不是对原代码进行修改。

假如我们现在有一批产品,每个产品都具有颜色和大小,产品其定义如下:

enum class Color { Red, Green, Blue };
enum class Size { Small, Medium, Large };

struct Product 
{
    string name;
    Color color;
    Size size;
};

这里 Product 定义为 struct 是因为 struct 默认的访问权限是公有方便书写,并且 struct 除了访问权限其他语法与 class 相同。

我们现在需要给一组产品提供过滤功能。于是定义下面的过滤器:

struct ProductFilter 
{
    using Items = vector<Product*>;
}

当我们需要针对 Color 的过滤时,我们增加方法 by_color:

struct ProductFilter
{
    using Items = vector<Product*>;
    // 新增方法 by_color
    Items by_color(Items items, Color color);
}

当我们需要针对 Size 的过滤时,我们增加方法 by_size:

struct ProductFilter
{
    using Items = vector<Product*>;
    Items by_color(Items items, Color color);
    // 新增方法 by_size
    Items by_size(Items items, Size size);
}

当我们需要针对 Color 和 Size 同时满足的筛选时,再添加…

可以看出当我们有新的需求时,必须要对 ProductFilter 类进行修改,并没有遵循开闭原则,所以我们希望重新设计使这个程序满足开闭原则,重构主要用到 template 模版编程。

首先,我们需要将过滤器分为两部分:过滤器本身和指定的过滤规范。

首先我们先定义一个规范接口,不同的过滤需求将通过继承此接口来满足:

template <typename T> 
struct Specification 
{
    virtual bool is_satisfied(T* item) = 0;
}

这里的类型 T 可以由我们自由地指定,我们可以指定为类型 Product 也可以指定为其他类型,这就意味着,这个规范将不再局限于 Product,我们可以在任何其他类型中使用它。

接下来是过滤器接口的定义:

template <typename T>
struct Filter
{
    virtual vector<T*> filter(vector<T*> items, Specification<T>& spec) const = 0;
}

同样地,这里使用模版编程来让过滤器不局限于对 Product 进行过滤。在虚函数 filter 中,我们接受 T 类型的容器,并通过 Specification 指定过滤规范。

然后我们需要继承 Filter 实现针对于 Product 的过滤器:


```cpp
struct BetterFilter: Filter<Product>
{
    vector<Product*> filter(vector<Product*> items, Specification<Product>& spec) const override {
        vector<Product*> result;
        for(auto& p: items) {
            if(spec.is_satisfied(p)) {
                result.push_back(p);
            }
        }
        return result;
    }
};

在 filter 方法中我们会调用 Specification& 中实现过滤规范对 vector<Product*> 容器中的对象进行筛选。

当我们有了以上的过滤器和规范接口之后,我们便可以在不修改代码的情况下,扩展业务了。

比如:当我们需要对于颜色的过滤器时,我们只需要继承 Specification 并覆盖 is_satisfied 方法来实现颜色的过滤法则,即可达到我们的目的:

// 颜色筛选规范
struct ColorSpecification : Specification<Product>
{
    Color color;
    explicit ColorSpecification(const Color& color) : color(color) {}
    bool is_satisfied(Product* item) override {
        return item->color == color;
    }
};

当我们需要针对 Size 的过滤时:

// 大小筛选规范
struct SizeSpecification : Specification<Product>
{
    Size size;
    explicit SizeSpecification(const Size& size) : size(size) {}
    bool is_satisfied(Product* item) override {
        return item->size == size;
    }
};

可以看到,我们不再需要修改过滤器来达到我们的目的,很显然我们遵从了开闭原则。

需要查看完整的示例代码可以访问 Github 仓库 GnCDesignPatterns

参考:C++20设计模式

  • 19
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值