美文网首页
使用concepts 代替虚函数实现开闭原则的filter

使用concepts 代替虚函数实现开闭原则的filter

作者: FredricZhu | 来源:发表于2022-04-16 18:14 被阅读0次

本例摘抄自《Design Patterns in Modern C++》,但是用concepts重写了虚函数的实现部分。
原例子是说需要对产品按照不同的属性进行过滤。如果直接写死filter,就需要写多个filter。如果写一个可以用于继承的specification,所有的specification都有一个is_satifisfied(Product*)方法,这样就可以对specification进行组合,按照不同的specification进行过滤。添加一个过滤规则,就是添加一个specification,不需要修改原来的代码,直接添加specification即可。对扩展开放,对修改封闭。
代码结构如下,


image.png

test/CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

if(APPLE)
    message(STATUS "This is Apple, do nothing.")
    set(CMAKE_MACOSX_RPATH 1)
    set(CMAKE_PREFIX_PATH /Users/aabjfzhu/software/vcpkg/ports/cppwork/vcpkg_installed/x64-osx/share )
elseif(UNIX)
    message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
    set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
endif(APPLE)

project(open_close)

set(CMAKE_CXX_STANDARD 20)

add_definitions(-g)

find_package(ZLIB)

find_package(OpenCV REQUIRED )
find_package(Arrow CONFIG REQUIRED)

find_package(unofficial-brotli REQUIRED)
find_package(unofficial-utf8proc CONFIG REQUIRED)
find_package(Thrift CONFIG REQUIRED)

find_package(glog REQUIRED)

find_package(OpenSSL REQUIRED)

find_package(Boost REQUIRED COMPONENTS
    system
    filesystem
    serialization
    program_options
    thread
    )

find_package(DataFrame REQUIRED)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
    set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../include/   ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/)
endif(APPLE)


if(APPLE)
    MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
    set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
endif(APPLE)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
    set(ODBC_LIBS iodbc iodbcinst)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(ODBC_LIBS odbc odbcinst ltdl)
endif(APPLE)

include_directories(${INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRS})

file( GLOB test_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) 

file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/arr_/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/death_handler/impl/*.cpp)

add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES} ${test_file})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib OpenSSL::SSL OpenSSL::Crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace libzstd.a libbz2.a libsnappy.a re2::re2 parquet lz4 unofficial::brotli::brotlidec-static unofficial::brotli::brotlienc-static unofficial::brotli::brotlicommon-static utf8proc thrift::thrift  arrow arrow_dataset)

foreach( test_file ${test_file_list} )
    file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${test_file})
    string(REPLACE ".cpp" "" file ${filename})
    add_executable(${file}  ${test_file})
    target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( test_file ${test_file_list})

test/open_close_test.cpp

#include "products.hpp"

#include "death_handler/death_handler.h"
#include "json/json.hpp"
#include <glog/logging.h>

#include <gtest/gtest.h>
#include "df/df.h"

using json = nlohmann::json;

int main(int argc, char** argv) {
    FLAGS_log_dir = "./";
    FLAGS_alsologtostderr = true;
    // 日志级别 INFO, WARNING, ERROR, FATAL 的值分别为0、1、2、3
    FLAGS_minloglevel = 0;

    Debug::DeathHandler dh;

    google::InitGoogleLogging("./logs.log");
    testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}

GTEST_TEST(OpenCloseTests, OpenClose) {
    Product apple {"Apple", Color::green, Size::small};
    Product tree {"Tree", Color::green, Size::large};
    Product house {"House", Color::blue, Size::large};

    std::vector<Product*> items {&apple, &tree, &house};
    // auto spec = AndSpec(ColorSpec(Color::green), SizeSpec(Size::large));

    auto spec = ColorSpec(Color::green) && SizeSpec(Size::large);
    
    auto bf = BetterFilter();
    for(auto& item: bf.filter(items, spec)) {
        std::cout << item->name << " is green and large\n";
    }
}

include/products.hpp

#ifndef _FREDRIC_PRODUCT_HPP_
#define _FREDRIC_PRODUCT_HPP_

#include <string>
#include <iostream>
#include <concepts>
#include <utility>
#include <vector>

enum class Color {red, green, blue};
enum class Size {small, medium, large};

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

template <typename T>
concept SpecType = requires (T value){
    {value.is_satisfied(std::declval<Product*>())} -> std::convertible_to<bool>;
};

struct BetterFilter {
    template <SpecType Specification>
    std::vector<Product*> filter(std::vector<Product*> const& items,Specification& spec) {
        std::vector<Product*> result;
        for(auto& item: items) {
            if(spec.is_satisfied(item)) {
                result.push_back(item);
            }
        }
        return result;
    }
};

struct ColorSpec {
    Color color;
    ColorSpec(Color color_): color{color_} {}

    bool is_satisfied(Product* item) {
        return item->color == color;
    }
};

struct SizeSpec {
    Size size;
    SizeSpec(Size size_): size{size_} {}

    bool is_satisfied(Product* item) {
        return item->size == size;
    }
};

template <SpecType Spec1, SpecType Spec2>
struct AndSpec {
    Spec1 first;
    Spec2 second;

    AndSpec(Spec1 first_, Spec2 second_): first{first_}, second{second_} {}

    bool is_satisfied(Product* item) {
        return first.is_satisfied(item) && second.is_satisfied(item);
    }
};

template <SpecType Spec1, SpecType Spec2>
AndSpec<Spec1, Spec2> operator&&(Spec1 const& spec1, Spec2 const& spec2) {
    return AndSpec<Spec1, Spec2>(spec1, spec2);
}
#endif

程序输出如下,


image.png

相关文章

  • 使用concepts 代替虚函数实现开闭原则的filter

    本例摘抄自《Design Patterns in Modern C++》,但是用concepts重写了虚函数的实现...

  • C++ 虚函数

    C++ 虚函数 虚函数 基类中使用virtual关键字声明的函数,称为虚函数。虚函数的实现,通过虚函数表来实现的。...

  • 软件设计七大原则

    1、开闭原则 开闭原则:一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。 用抽象构建框架,用实现扩展细节。...

  • 设计模式之软件开发原则(1)开闭原则和依赖倒置原则

    开闭原则 定义 所谓开闭原则就是一个软件实体如类、模块和函数应该对扩展开放、对修改关闭。强调用抽象构建框架,实现实...

  • iOS开闭原则和替换原则

    什么叫开闭原则? 六大模式设计原则之一的开闭原则是比较重要的,开闭原则其实就是“抽象构建框架,实现扩展细节...

  • 高阶函数的使用

    filter/map/reduce 这三个高阶函数都比较常用,一般代替for循环使用filter也是一个常用的操作...

  • js数组中filter、map、reduce、find等方法实现

    filter用法和原理实现 filter 过滤,filter()使用指定的函数测试所有元素,并创建一个包含所有通过...

  • 软件设计原则

    开闭原则 定义:类、模块、函数应该对拓展开放,对修改关闭。 用抽象构建框架,用实现拓展细节。 依赖倒置原则 定义:...

  • 使用concepts实现接口隔离原则

    本例有一个打印机类,部分打印机可打印,部分打印机可扫描,部分打印机可传真。作者本想设计一个大的接口,用来同时pri...

  • python和设计模式18

    为什么说开闭原则是单一职责原则的一个特例呢? 因为如果想很好的实现开闭原则的话,把职责分清是一个前提。 开闭原则讲...

网友评论

      本文标题:使用concepts 代替虚函数实现开闭原则的filter

      本文链接:https://www.haomeiwen.com/subject/iwyhertx.html