从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设计模式

相关推荐
Code_流苏1 小时前
C++课设:智能优惠快餐点餐系统
开发语言·c++·课设·期末大作业·快餐点餐系统·智能优惠算法
越城1 小时前
深入解析C++引用:从别名机制到函数特性实践
c++
qwertyuiop_i2 小时前
pe文件结构(TLS)
c++·tls·pe文件结构
岁忧3 小时前
(nice!!!)(LeetCode每日一题)2434. 使用机器人打印字典序最小的字符串(贪心+栈)
java·c++·算法·leetcode·职场和发展·go
无敌的小笼包3 小时前
第四讲:类和对象(下)
数据结构·c++
鑫鑫向栄4 小时前
[蓝桥杯]解谜游戏
数据结构·c++·算法·职场和发展·蓝桥杯
闻缺陷则喜何志丹5 小时前
【分治法 容斥原理 矩阵快速幂】P6692 出生点|普及+
c++·线性代数·数学·洛谷·容斥原理·分治法·矩阵快速幂
鑫鑫向栄5 小时前
[蓝桥杯]整理玩具
数据结构·c++·算法·蓝桥杯·动态规划
南郁15 小时前
007-nlohmann/json 项目应用-C++开源库108杰
c++·开源·json·nlohmann·现代c++·d2school·108杰
菠萝0116 小时前
共识算法Raft系列(1)——什么是Raft?
c++·后端·算法·区块链·共识算法