Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Interface Bridging with Adapter | Structural Patterns
Quizzes & Challenges
Quizzes
Challenges
/
C++ Design Patterns

bookInterface 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

main.cpp

copy
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.

question mark

What is the main purpose of the Adapter pattern in software design?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 10

bookInterface Bridging with Adapter

Svep för att visa menyn

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

main.cpp

copy
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.

question mark

What is the main purpose of the Adapter pattern in software design?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt