From the course: C++ Design Patterns: Structural
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
When should you use the Adapter pattern? - C++ Tutorial
From the course: C++ Design Patterns: Structural
When should you use the Adapter pattern?
- [Instructor] Let me show you a scenario where the adapter design pattern may come in handy. Say that you're working on a project that contains an abstract class called component. It exposes a pure virtual method called run that must be implemented by sub-classes. Concrete component A and concrete component B inherit from component and implement the run method. The main function creates an array of component pointers initialized with one smart pointer of concrete component A and one of concrete component B. Afterward, the code iterates over each element in the array and cause its run method. This is possible because of polymorphism. Both concrete component A and concrete component B inherit from component, exposing the same run method. The run method is resolved at runtime depending on the actual type of each pointer, so this code will execute just fine. And here in the console, we can see the result executing concrete…