Last active
February 22, 2023 13:24
-
-
Save nboutin/d8f442ba6eadadd179f436563dd2ee98 to your computer and use it in GitHub Desktop.
Dependency injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.youtube.com/watch?v=zYPb7oBU5_E | |
#include "time_interface.h" | |
#include "output_interface.h" | |
class alarm_clock { | |
public: | |
alarm_clock(const time_interface&, output_interface&) | |
// ... | |
private: | |
// ... | |
const time_interface& time_; | |
output_interface& output_; | |
}; | |
void alarm_clock::check_alarm() { | |
if (!is_active_) return; | |
if (now_is_approx_alarm_time()) { | |
ring_alarm(); | |
} | |
} | |
bool alarm_clock::now_is_approx_alarm_time() const { | |
auto diff = abs(time_.now() - alarm_time_); | |
return (diff < 1s); | |
} | |
void alarm_clock::ring_alarm() { | |
output_ << "Alarm\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
time_impl t; // implements the time_interface | |
output_console o; // implements the output_interface | |
alarm_clock alarm(t, o); | |
// ... | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class output_interface { | |
public: | |
virtual ~output_interface() = default; | |
virtual output_interface& operator<<(const string&) = 0; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEST(alarm, checkAlarmAfter10Minutes) { | |
time_fake t; // implements the time_interface | |
output_fake o; // implements the output interface | |
alarm_clock alarm(t, o); | |
auto ring_time = t.now() + 10min; | |
clock.set_alarm(ring_time); | |
t = t.now() + 10min; // We can increase our time | |
EXPECT_EQ(o.value(), "Alarm\n"); // We can check the output | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
class time_interface { | |
public: | |
using time_point = std::chrono::time_point<std::chrono::system_clock>; | |
virtual ~time_interface() = default; | |
virtual time_point now() const = 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment