File tree Expand file tree Collapse file tree 2 files changed +101
-1
lines changed Expand file tree Collapse file tree 2 files changed +101
-1
lines changed Original file line number Diff line number Diff line change @@ -21,4 +21,5 @@ set(CMAKE_CXX_STANDARD 20)
21
21
#add_executable(lspcovarient LSP/lsp_covariance_example.cpp)
22
22
#add_executable(lspqueryset LSP/lsp_query_set.cpp)
23
23
#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 )
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments