Adapter pattern works as a bridge between two incompatible interfaces. It converts interface A into another interface B so that interface A could be compatible with those class of interface B. A real life example could be a case of travaling to another country, in the hotel, you want to charge your mobile, but you find that the socket is absolutly not the same as the ones of your country. So now we need an adapter to charge your mobile.
We have interfaces
Turkey. Now we want an Adapter to convert
Duck, so in this way, on client side, we see only Ducks! (So turkey classes would be compatible with methods for interface
To realize an adapter, we have two ways:
- Object Adapter - uses composition and can wrap classes or interfaces, or both. It can do this since it contains, as a private, encapsulated member, the class or interface object instance it wraps.
- Class Adapter - uses inheritance and can only wrap a class. It cannot wrap an interface since by definition it must derive from some base class.
So on client side, we only see a duck! But it performs like a turkey :D