Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Interface Bridging with Adapter | Structural Patterns
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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 10

bookInterface Bridging with Adapter

Deslize para mostrar o 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

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
some-alt