Tag: Design Patterns
11 posts
-
C++ RAII Explained | Why Constructors and Destructors Prevent Leaks
A deep dive into C++ RAII grounded in a real production connection-leak incident, covering exception safety, the C++ Core Guidelines, and composable ownership.
-
JavaScript Design Patterns: Singleton, Factory, Module, Observer, Proxy and Strategy
Singleton, Factory, Module, Observer, Proxy and Strategy patterns in JavaScript, with ES6 code, an event system, a caching proxy and a small state store.
-
C++ Visitor Pattern: Classic Double Dispatch vs std::variant and std::visit
Visitor pattern in modern C++: classic accept/visit vs std::variant + std::visit (C++17), ASTs, the expression-problem tradeoff between adding types and adding operations, and the pitfalls in each.
-
Type Erasure in C++: How std::function and std::any Work, and Building Your Own
How std::function and std::any hide concrete types behind one interface, building your own copyable type-erased wrapper, and its cost versus virtuals.
-
The Strategy Pattern in C++: Virtual Classes vs Function Pointers vs Lambdas vs std::function
Strategy pattern in C++: polymorphic strategies, function pointers, lambdas, std::function—sorting and compression examples; performance trade-offs.
-
The State Pattern in C++: State Objects, Safe Transitions and std::variant
The State pattern in C++: replacing switch chains with state classes, avoiding the delete-this trap during transitions, shared states, and a std::variant FSM.
-
The Pimpl Idiom: Out-of-Line Destructors, unique_ptr<Impl> and the Build-Time vs Runtime Trade-off
Why the Pimpl destructor must be defined out-of-line, why unique_ptr<Impl> forces the full Rule of Five, and how Pimpl trades compile-time decoupling for indirect-call overhead — with a working fix for the classic Fast-Pimpl placement-new mistake.
-
The Observer Pattern in C++: weak_ptr Subscribers, Typed Events and Signal/Slot Designs
Observer pattern in C++: decouple publishers and subscribers, weak_ptr, typed events, signal/slot style—patterns, pitfalls, and production examples.
-
CRTP vs Virtual Functions: Static Polymorphism Without vtable Overhead
How CRTP gives C++ static polymorphism without a vtable, how it compares with virtual functions, the usual CRTP mistakes, and C++23 deducing this.
-
Factories in C++: Simple Factory vs Factory Method vs Abstract Factory, and Self-Registering Plugins
Simple Factory, Factory Method and Abstract Factory in C++ with unique_ptr, plus self-registering plugin factories and why static libraries silently drop them.
-
The Adapter Pattern in C++: Object vs Class Adapters for Legacy and Third-Party APIs
Adapter pattern in C++: object vs class adapter, payment/API examples, unique_ptr—bridge incompatible interfaces for legacy and third-party code.