Skip to content

Instantly share code, notes, and snippets.

@nboutin
Last active February 22, 2023 13:24
Show Gist options
  • Select an option

  • Save nboutin/d8f442ba6eadadd179f436563dd2ee98 to your computer and use it in GitHub Desktop.

Select an option

Save nboutin/d8f442ba6eadadd179f436563dd2ee98 to your computer and use it in GitHub Desktop.

Revisions

  1. nboutin revised this gist Feb 22, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions test.cpp
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,10 @@ 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
    }
  2. nboutin revised this gist Feb 22, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions dependency_injection.cpp
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    #include "time_interface.h"
    #include "output_interface.h"

    class alarm_clock {
    public:
    alarm_clock(const time_interface&, output_interface&)
  3. nboutin created this gist Feb 22, 2023.
    29 changes: 29 additions & 0 deletions dependency_injection.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // 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";
    }
    7 changes: 7 additions & 0 deletions main.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    int main() {
    time_impl t; // implements the time_interface
    output_console o; // implements the output_interface
    alarm_clock alarm(t, o);
    // ...
    return 0;
    }
    5 changes: 5 additions & 0 deletions output_interface.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    class output_interface {
    public:
    virtual ~output_interface() = default;
    virtual output_interface& operator<<(const string&) = 0;
    };
    7 changes: 7 additions & 0 deletions test.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    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);
    }
    9 changes: 9 additions & 0 deletions time_interface.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #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;
    };