这本书在讲到Open-Close Principle的时候,举的例子是如果在vector<Product *>
上增加不同的过滤条件,那么应该把specification<T>
和filter<T>
都作为纯虚基类,specification<T>
判断是否满足条件,filter<T>
做过滤动作
但是,从modern C++的角度来看,似乎有更省力(一个模版就行),并且运行效率更高(干掉动态继承),而且一样通用的办法
template <typename Container, typename UnaryPredicate>
auto filter(const Container &items, UnaryPredicate