Interface Bridging with Adapter
The Adapter pattern is a structural design pattern that enables classes with incompatible interfaces to work together. Its primary purpose is to bridge the gap between legacy code and new systems by converting the interface of a class into another interface that clients expect. This approach is particularly valuable when you need to integrate existing componentsβoften with well-established and unchangeable APIsβinto modern architectures that rely on different interfaces. By introducing an adapter, you avoid modifying the legacy code directly, reducing risk and preserving stability, while still achieving interoperability and code reuse.
main.cpp
12345678910111213141516171819202122232425262728293031323334353637383940#include <iostream> #include <string> // Legacy class with an incompatible interface class LegacyPrinter { public: void printOld(const std::string& text) { std::cout << "LegacyPrinter: " << text << std::endl; } }; // Target interface expected by new code class IPrinter { public: virtual void print(const std::string& text) = 0; virtual ~IPrinter() = default; }; // Adapter: makes LegacyPrinter compatible with IPrinter class PrinterAdapter : public IPrinter { LegacyPrinter* legacyPrinter; public: PrinterAdapter(LegacyPrinter* lp) : legacyPrinter(lp) {} void print(const std::string& text) override { legacyPrinter->printOld(text); } }; int main() { LegacyPrinter legacy; PrinterAdapter adapter(&legacy); // Client code uses the new IPrinter interface IPrinter* printer = &adapter; printer->print("Hello, Adapter Pattern!"); }
Adapters are essential in many real-world scenarios where software systems evolve over time. Large enterprises often have legacy systems with stable, time-tested APIs that cannot be easily changed or rewritten. When new business requirements demand integration with these systems, adapters allow you to introduce new features or connect with modern frameworks without risking the reliability of core components.
Adapters are also commonly used when incorporating third-party libraries, migrating to new protocols, or supporting multiple interface standards within the same application. By acting as a bridge, the Adapter pattern ensures that system evolution can proceed smoothly and incrementally, enabling interoperability and reducing the risk of introducing errors into proven code.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 10
Interface Bridging with Adapter
Swipe to show menu
The Adapter pattern is a structural design pattern that enables classes with incompatible interfaces to work together. Its primary purpose is to bridge the gap between legacy code and new systems by converting the interface of a class into another interface that clients expect. This approach is particularly valuable when you need to integrate existing componentsβoften with well-established and unchangeable APIsβinto modern architectures that rely on different interfaces. By introducing an adapter, you avoid modifying the legacy code directly, reducing risk and preserving stability, while still achieving interoperability and code reuse.
main.cpp
12345678910111213141516171819202122232425262728293031323334353637383940#include <iostream> #include <string> // Legacy class with an incompatible interface class LegacyPrinter { public: void printOld(const std::string& text) { std::cout << "LegacyPrinter: " << text << std::endl; } }; // Target interface expected by new code class IPrinter { public: virtual void print(const std::string& text) = 0; virtual ~IPrinter() = default; }; // Adapter: makes LegacyPrinter compatible with IPrinter class PrinterAdapter : public IPrinter { LegacyPrinter* legacyPrinter; public: PrinterAdapter(LegacyPrinter* lp) : legacyPrinter(lp) {} void print(const std::string& text) override { legacyPrinter->printOld(text); } }; int main() { LegacyPrinter legacy; PrinterAdapter adapter(&legacy); // Client code uses the new IPrinter interface IPrinter* printer = &adapter; printer->print("Hello, Adapter Pattern!"); }
Adapters are essential in many real-world scenarios where software systems evolve over time. Large enterprises often have legacy systems with stable, time-tested APIs that cannot be easily changed or rewritten. When new business requirements demand integration with these systems, adapters allow you to introduce new features or connect with modern frameworks without risking the reliability of core components.
Adapters are also commonly used when incorporating third-party libraries, migrating to new protocols, or supporting multiple interface standards within the same application. By acting as a bridge, the Adapter pattern ensures that system evolution can proceed smoothly and incrementally, enabling interoperability and reducing the risk of introducing errors into proven code.
Thanks for your feedback!