The Adapter Pattern (sometimes called the wrapper pattern) allows objects satisfying one interface to be used where another type of interface is expected. There are two typical flavours of the pattern: the delegation flavour and the inheritance flavour.
Suppose we have the following classes:
We can ask the
RoundHole class if a
RoundPeg fits in it, but if we ask the same question for a
SquarePeg, then it will fail because the
SquarePeg class doesn't have a
radius property (i.e. doesn't satisfy the required interface).
To get around this problem, we can create an adapter to make it appear to have the correct interface. It would look like this:
We can use the adapter like this:
Which results in the following output: