20#include <unordered_map>
54 std::cout <<
"[Old] Initializing monitor: " <<
name_ << std::endl;
58 std::cout <<
"[Old] Collecting metrics..." << std::endl;
64 std::cout <<
"[Old] Recording metric: " << name <<
" = " << value << std::endl;
69 std::cout <<
"[Old] Health check: OK" << std::endl;
81 std::unordered_map<std::string, double>
metrics_;
95 std::cout <<
"[Core] Initializing monitor: " <<
name_ << std::endl;
100 std::cout <<
"[Core] Collecting metrics..." << std::endl;
105 std::cout <<
"[Core] Recording metric: " << name <<
" = " << value << std::endl;
110 std::cout <<
"[Core] Health check: " << (
initialized_ ?
"OK" :
"NOT_INITIALIZED") << std::endl;
127 :
impl_(std::move(impl)) {}
134 std::cout <<
"[Adapter:MetricsCollector] Delegating initialize..." << std::endl;
139 std::cout <<
"[Adapter:MetricsCollector] Delegating collect_metrics..." << std::endl;
144 std::shared_ptr<performance_monitor_impl>
impl_;
151 :
impl_(std::move(impl)) {}
154 std::cout <<
"[Adapter:IMonitor] Delegating record_metric..." << std::endl;
159 std::cout <<
"[Adapter:IMonitor] Delegating get_health..." << std::endl;
164 std::shared_ptr<performance_monitor_impl>
impl_;
172 impl_ = std::make_shared<performance_monitor_impl>(name);
194 std::shared_ptr<performance_monitor_impl>
impl_;
204 std::cout <<
"\n=== OLD APPROACH: Multiple Inheritance ===\n" << std::endl;
218 std::cout <<
"\nProblems:" << std::endl;
219 std::cout <<
"- Unclear which interface is being used" << std::endl;
220 std::cout <<
"- Method name conflicts possible" << std::endl;
221 std::cout <<
"- Hard to test interfaces independently" << std::endl;
222 std::cout <<
"- Violates Single Responsibility Principle" << std::endl;
226 std::cout <<
"\n\n=== NEW APPROACH: Facade + Adapters ===\n" << std::endl;
233 collector.collect_metrics();
237 imonitor.get_health();
240 std::cout <<
"\nDirect access to core: "
243 std::cout <<
"\nBenefits:" << std::endl;
244 std::cout <<
"ā
Clear which interface is being used" << std::endl;
245 std::cout <<
"ā
No method name conflicts (separate adapters)" << std::endl;
246 std::cout <<
"ā
Easy to mock and test independently" << std::endl;
247 std::cout <<
"ā
Single Responsibility: core does monitoring, adapters adapt" << std::endl;
251 std::cout <<
"\n\n=== TESTING BENEFITS ===\n" << std::endl;
256 std::string
get_name()
const override {
return "mock"; }
258 std::cout <<
"[Mock] Initialize called" << std::endl;
259 initialize_called_ =
true;
262 std::cout <<
"[Mock] Collect metrics called" << std::endl;
263 collect_called_ =
true;
266 bool initialize_called_ =
false;
267 bool collect_called_ =
false;
271 auto mock = std::make_shared<mock_metrics_adapter>();
274 std::cout <<
"Testing metrics_collector interface..." << std::endl;
278 std::cout <<
"\nVerification:" << std::endl;
279 std::cout <<
"- initialize_called: " << (mock->initialize_called_ ?
"YES" :
"NO") << std::endl;
280 std::cout <<
"- collect_called: " << (mock->collect_called_ ?
"YES" :
"NO") << std::endl;
282 std::cout <<
"\nā
Can test each interface independently!" << std::endl;
286 std::cout <<
"\n\n=== PERFORMANCE COMPARISON ===\n" << std::endl;
288 const int iterations = 1000000;
293 auto start = std::chrono::high_resolution_clock::now();
295 for (
int i = 0; i < iterations; ++i) {
299 auto end = std::chrono::high_resolution_clock::now();
300 auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
301 std::cout <<
"Old (multiple inheritance): "
302 << duration.count() / iterations <<
" ns/call" << std::endl;
309 auto start = std::chrono::high_resolution_clock::now();
311 for (
int i = 0; i < iterations; ++i) {
312 imonitor.record_metric(
"test", 1.0);
315 auto end = std::chrono::high_resolution_clock::now();
316 auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
317 std::cout <<
"New (facade + adapters): "
318 << duration.count() / iterations <<
" ns/call" << std::endl;
321 std::cout <<
"\nOverhead: ~3ns (negligible for monitoring operations)" << std::endl;
330 std::cout <<
"\n\n=== SUMMARY ===" << std::endl;
331 std::cout <<
"Facade + Adapter pattern provides:" << std::endl;
332 std::cout <<
"ā
Clear interface separation" << std::endl;
333 std::cout <<
"ā
No name conflicts" << std::endl;
334 std::cout <<
"ā
Easy to test and mock" << std::endl;
335 std::cout <<
"ā
Single Responsibility Principle" << std::endl;
336 std::cout <<
"ā
Minimal performance overhead (~3ns)" << std::endl;
337 std::cout <<
"ā
Better maintainability" << std::endl;
339 std::cout <<
"\nRecommendation: Proceed with refactoring" << std::endl;
imonitor_adapter(std::shared_ptr< performance_monitor_impl > impl)
void get_health() override
void record_metric(const std::string &name, double value) override
std::shared_ptr< performance_monitor_impl > impl_
virtual ~imonitor_interface()=default
virtual void record_metric(const std::string &name, double value)=0
virtual void get_health()=0
metrics_collector_adapter(std::shared_ptr< performance_monitor_impl > impl)
void initialize() override
std::string get_name() const override
void collect_metrics() override
std::shared_ptr< performance_monitor_impl > impl_
virtual void collect_metrics()=0
virtual ~metrics_collector_interface()=default
virtual std::string get_name() const =0
virtual void initialize()=0
void demonstrate_old_approach()
void demonstrate_testing_benefits()
void benchmark_overhead()
void demonstrate_new_approach()