Skip to content

Commit d3c6ad6

Browse files
author
sajith
committed
Improved
1 parent bb5bb15 commit d3c6ad6

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ set(CMAKE_CXX_STANDARD 20)
2121
#add_executable(lspcovarient LSP/lsp_covariance_example.cpp)
2222
#add_executable(lspqueryset LSP/lsp_query_set.cpp)
2323
#add_executable(isp_door ISP/isp_door_example.cpp)
24-
add_executable(isp_door_better ISP/isp_door_example_better.cpp)
24+
#add_executable(isp_door_better ISP/isp_door_example_better.cpp)
25+
add_executable(isp_door_better2 ISP/isp_door_example_better2.cpp)

ISP/isp_door_example_better2.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
//
3+
// Created by sajith on 4/18/21.
4+
//
5+
6+
#include <iostream>
7+
#include <optional>
8+
#include <stdexcept>
9+
#include <memory>
10+
11+
class TimerClient
12+
{
13+
public:
14+
virtual ~TimerClient() = default;
15+
virtual void timeout() = 0;
16+
};
17+
18+
class Door
19+
{
20+
public:
21+
virtual ~Door() = default;
22+
virtual void open() = 0;
23+
virtual void close() = 0;
24+
};
25+
26+
class Timer
27+
{
28+
TimerClient &client;
29+
30+
public:
31+
Timer(TimerClient &client) : client{client}
32+
{
33+
client.timeout();
34+
}
35+
};
36+
37+
class TimedDoorAdapter;
38+
39+
40+
class TimedDoor : public Door
41+
{
42+
std::unique_ptr<TimedDoorAdapter> adapter;
43+
public:
44+
void open()
45+
{
46+
std::cout << "Open\n";
47+
adapter = std::make_unique<TimedDoorAdapter>(*this);
48+
}
49+
50+
void close()
51+
{
52+
std::cout << "Close\n";
53+
}
54+
55+
void timeout()
56+
{
57+
std::cout << "Timeout\n";
58+
close();
59+
adapter = nullptr;
60+
}
61+
};
62+
63+
class NormalDoor : public Door
64+
{
65+
public:
66+
void open()
67+
{
68+
std::cout << "Open\n";
69+
}
70+
71+
void close()
72+
{
73+
std::cout << "Close\n";
74+
}
75+
};
76+
77+
class TimedDoorAdapter : public TimerClient
78+
{
79+
TimedDoor &door;
80+
Timer timer;
81+
82+
public:
83+
TimedDoorAdapter(TimedDoor &door) : door{door}, timer{Timer{*this}} {}
84+
85+
void timeout()
86+
{
87+
door.timeout();
88+
}
89+
};
90+
91+
int main()
92+
{
93+
TimedDoor door;
94+
door.open();
95+
door.open();
96+
97+
return 0;
98+
}
99+

0 commit comments

Comments
 (0)