|
| 1 | +// High-level modules should not depend upon low-level modules. Both should depend upon abstractions. |
| 2 | +// Abstractions should not depend upon details. Details should depend upon abstractions. |
| 3 | + |
| 4 | +/** |
| 5 | + * @file dependency_inversion.cpp |
| 6 | + * @author Xuhua Huang |
| 7 | + * @brief Demonstration of the Dependency Inversion Principle (DIP) |
| 8 | + * |
| 9 | + * High-level modules should not depend on low-level modules. |
| 10 | + * Both should depend on abstractions. |
| 11 | + * |
| 12 | + * @version 0.1 |
| 13 | + * @date 2026-04-09 |
| 14 | + */ |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +// Abstractions |
| 21 | +class data_source_t { |
| 22 | +public: |
| 23 | + virtual ~data_source_t() = default; |
| 24 | + virtual std::string read() const = 0; |
| 25 | +}; |
| 26 | + |
| 27 | +class report_format_t { |
| 28 | +public: |
| 29 | + virtual ~report_format_t() = default; |
| 30 | + virtual void write(const std::string& data) const = 0; |
| 31 | +}; |
| 32 | + |
| 33 | +// Low-Level Implementations (Depend on Abstractions) |
| 34 | +class database_source_t : public data_source_t { |
| 35 | +public: |
| 36 | + std::string read() const override { return "data from database"; } |
| 37 | +}; |
| 38 | + |
| 39 | +class text_file_source_t : public data_source_t { |
| 40 | +public: |
| 41 | + std::string read() const override { return "data from text file"; } |
| 42 | +}; |
| 43 | + |
| 44 | +class html_report_writer_t : public report_format_t { |
| 45 | +public: |
| 46 | + void write(const std::string& data) const override { std::cout << "<html><body>" << data << "</body></html>\n"; } |
| 47 | +}; |
| 48 | + |
| 49 | +class pdf_report_writer_t : public report_format_t { |
| 50 | +public: |
| 51 | + void write(const std::string& data) const override { std::cout << "PDF Report: " << data << '\n'; } |
| 52 | +}; |
| 53 | + |
| 54 | +// High-Level Module (Depends Only on Abstractions) |
| 55 | +class report_generator_t { |
| 56 | +public: |
| 57 | + report_generator_t(std::unique_ptr<data_source_t> source, std::unique_ptr<report_format_t> formatter) |
| 58 | + : source(std::move(source)) |
| 59 | + , formatter(std::move(formatter)) {} |
| 60 | + |
| 61 | + void generate_report() const { |
| 62 | + std::string data = source->read(); |
| 63 | + formatter->write(data); |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + std::unique_ptr<data_source_t> source; |
| 68 | + std::unique_ptr<report_format_t> formatter; |
| 69 | +}; |
| 70 | + |
| 71 | +// Composition Root (Client Code) |
| 72 | +int main() { |
| 73 | + // Example 1: Database + HTML |
| 74 | + report_generator_t generator(std::make_unique<database_source_t>(), std::make_unique<html_report_writer_t>()); |
| 75 | + |
| 76 | + generator.generate_report(); |
| 77 | + |
| 78 | + std::cout << "------------------------\n"; |
| 79 | + |
| 80 | + // Example 2: Text File + PDF |
| 81 | + report_generator_t generator2(std::make_unique<text_file_source_t>(), std::make_unique<pdf_report_writer_t>()); |
| 82 | + |
| 83 | + generator2.generate_report(); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments