使用concepts 实现符合里氏替换原则的Rect

本例演示使用 concepts实现里氏替换原则。
例子中本来是从Rectangle类继承出一个Square类,但是后来发现在process方法中无法实现使用square替换rectangle,会导致结果不一致。
所以在Rectangle类中写了一个工厂类,用来创建Rect和Square,同时使用concepts约束Rectangle类的行为。
代码如下,
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(lisk_substitu)

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/lisk_substitu_test.cpp

#include "rectangle.hpp"

#include "death_handler/death_handler.h"
#include "json/json.hpp"
#include 

#include 
#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(LiskSubstituTests, LiskSubstitu) {
    auto rect = Rect::Factory::create_rect(3, 4);
    // width: 3 height: 10 -> 30
    process(rect);
    // width: 5 height: 10 -> 50
    auto square = Rect::Factory::create_square(5);
    process(square);
}

include/rectangle.hpp

#ifndef _FREDRIC_RECTANGLE_HPP_
#define _FREDRIC_RECTANGLE_HPP_

#include 
#include 
#include 

template 
concept RectGetType = requires (const T value) {
    {value.get_width()} -> std::convertible_to;
    {value.get_height()} -> std::convertible_to;
    {value.area()} -> std::convertible_to;
};

template 
concept RectSetType = requires (T value) {
    value.set_width(std::declval());
    value.set_height(std::declval());
};

template 
concept RectType = RectGetType && RectSetType;

struct Rect {
    class Factory {
        public:
            static Rect create_rect(int width, int height) {
                return Rect{width, height};
            }
            static Rect create_square(int size) {
                return Rect{size, size};
            }
    };
    private:
        Rect(int width_, int height_): width{width_}, height{height_} {}
    protected:
        int width, height;
    public:
       
        int get_width() const {
            return width;
        }

        void set_width(int width) {
            this->width = width;
        } 

        int get_height() const {
            return height;
        }

        void set_height(int height) {
            this->height = height;
        } 

        int area() const {
            return width * height;
        }
};



template 
void process(RectT& r) {
    // 子类不能替换父类,违背了 里氏替换原则
    int width = r.get_width();
    r.set_height(10);
    std::cout << "Expect area = " << (width * 10) 
     << " got " << r.area() << "\n";
}

#endif

程序输出如下,


image.png

你可能感兴趣的:(使用concepts 实现符合里氏替换原则的Rect)