A component exists that provides the functionality required by a client, but doesn't implement the interface expected by the client.
Create an adapter that sits between the client and the component. The adapter translates client requests into a messages understood by the client and vice-versa.
The adapter can inherit from the component:
Or the adapter can delegate to the component:
The client expects a component that implements the IComponent interface:
class Client {
private IComponent component;
public task() {
component.serviceA();
component.serviceB();
}
}
Where:
interface IComponent {
void serviceA();
void serviceB();
}
A particular component provides the functionality needed by Client, but not the interfaces:
class Component {
void serviceX() { /* serviceA
functionality */ }
void serviceY() { /* serviceB
functionality */ }
}
An Adapter implements the IComponent interface by invoking inherited Component methods:
class Adapter extends Component implements IComponent {
public void serviceA() { serviceX(); }
public void serviceB() { serviceY(); }
}
Alternatively, delegation can be used:
class Adapter implements IComponent {
private Component component = new
Component();
public void serviceA() {
component.serviceX(); }
public void serviceB() {
component.serviceY(); }
}