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

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

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

cpp 复制代码
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 相同。

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

cpp 复制代码
struct ProductFilter 
{
    using Items = vector<Product*>;
}

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

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

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

cpp 复制代码
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 模版编程。

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

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

cpp 复制代码
template <typename T> 
struct Specification 
{
    virtual bool is_satisfied(T* item) = 0;
}

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

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

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

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

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

cpp 复制代码
```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 方法来实现颜色的过滤法则,即可达到我们的目的:

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

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

cpp 复制代码
// 大小筛选规范
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设计模式

相关推荐
C+++Python3 小时前
C++ 进阶学习完整指南
java·c++·学习
sparEE4 小时前
c++值类别、右值引用和移动语义
开发语言·c++
jrrz08285 小时前
Apollo MPC Controller
c++·自动驾驶·apollo·mpc·横向控制·lateral control
小王C语言7 小时前
【线程概念与控制】:线程封装
jvm·c++·算法
学习,学习,在学习7 小时前
Qt工控仪器程序框架设计详解(工控多仪器控制版本)
开发语言·c++·qt
信竞星球_少儿编程题库7 小时前
2026年全国信息素养大赛算法应用主题赛 丝路新城 C++ 模拟卷(三)
开发语言·c++
Zhang~Ling7 小时前
深入解析C++list:从0到1实现一个完整的链表类
c++·链表·list
王老师青少年编程8 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2015 提高组] 子串
c++·字符串·csp·高频考点·子串·信奥赛
June`8 小时前
redis项目之命令解析器
数据库·c++·redis
rGzywSmDg9 小时前
如何在Dev-C++中设置TDM-GCC为默认编译器
开发语言·c++