Skip to content

Commit e87e4e4

Browse files
xiao chengxiao cheng
xiao cheng
authored and
xiao cheng
committed
adaptor pattern
1 parent 7d935ca commit e87e4e4

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Adaptor/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
3+
project(Adaptor_test)
4+
5+
include_directories(${Boost_INCLUDE_DIRS})
6+
7+
add_executable(${PROJECT_NAME} adaptorTest.cpp)
8+
9+
target_link_libraries(${PROJECT_NAME} PUBLIC
10+
gtest_main
11+
${Boost_LIBRARIES}
12+
)
13+
14+
add_test(
15+
NAME ${PROJECT_NAME}
16+
COMMAND ${PROJECT_NAME}
17+
)
18+
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ../bin)

Adaptor/adaptorTest.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
struct Square
2+
{
3+
int side{0};
4+
5+
explicit Square(const int side)
6+
: side(side)
7+
{
8+
}
9+
};
10+
11+
struct Rectangle
12+
{
13+
virtual int width() const = 0;
14+
virtual int height() const = 0;
15+
16+
int area() const
17+
{
18+
return width() * height();
19+
}
20+
};
21+
22+
struct SquareToRectangleAdapter : Rectangle
23+
{
24+
SquareToRectangleAdapter(const Square &square)
25+
: square(square) {}
26+
27+
int width() const override
28+
{
29+
return square.side;
30+
}
31+
32+
int height() const override
33+
{
34+
return square.side;
35+
}
36+
37+
const Square □
38+
};
39+
40+
#include "gtest/gtest.h"
41+
42+
//#include "helpers/iohelper.h"
43+
44+
//#include "exercise.cpp"
45+
46+
namespace
47+
{
48+
class Evaluate : public testing::Test
49+
{
50+
};
51+
52+
TEST_F(Evaluate, SimpleTest)
53+
{
54+
Square sq{11};
55+
SquareToRectangleAdapter adapter{sq};
56+
ASSERT_EQ(121, adapter.area());
57+
}
58+
} // namespace
59+
60+
int main(int ac, char *av[])
61+
{
62+
testing::InitGoogleTest(&ac, av);
63+
return RUN_ALL_TESTS();
64+
}

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ if(Boost_FOUND)
2020
add_subdirectory(ObjectPool)
2121
add_subdirectory(RAII)
2222
add_subdirectory(Decorator)
23+
add_subdirectory(Adaptor)
2324
endif()

0 commit comments

Comments
 (0)