8th Light: Day 90

06 May 2016 . 8thlight . Comments #apprenticeship #8thlight #software #adapter #pattern #patterns

Adapter Pattern

Software design pattern that allows the interface of an existing class to be used as another interface - Wikipedia

Adapters in the real world

Imagine you are visiting Europe from the United States with your laptop that uses a US power supply. In order for the laptop to work in Europe, a power adapter is needed to accept the US plug while also being able to plug into the European power outlet. This adapter acts as the “middleman” between both sides and makes two things work together that could not otherwise because of incompatible interfaces. This is the adapter pattern.

Adapters in software

The adapter pattern is a structural pattern that acts as a “middleman”, “glue”, or “wrapper”. Unlike the bridge pattern, the adapter pattern makes things work after they are designed – for instance in the case of matching an old component to a new system.

UML example

In the image below, the WizardAdapter acts as a middleman between the Fighter interface and the Wizard class, allowing the Wizard to behave and be used as Fighter

Adapter pattern example