The adapter pattern
The adapter pattern is a structural design pattern that helps us make two incompatible interfaces compatible. What does that really mean? If we have an old component and we want to use it in a new system, or a new component that we want to use in an old system, the two can rarely communicate without requiring any code changes. But changing the code is not always possible, either because we don’t have access to it, or because it is impractical. In such cases, we can write an extra layer that makes all the required modifications for enabling communication between the two interfaces. This layer is called an adapter.
In general, if you want to use an interface that expects function_a()
, but you only have function_b()
, you can use an adapter to convert (adapt) function_b()
to function_a()
.
Real-world examples
When you are traveling from most European countries to the UK or the USA, or the other way around, you need to use a plug adapter for charging your...