Wednesday 29 July 2015

The Adapter Patterm

The Adapter Pattern

Defined

The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Diagrams










Despite having defined the pattern, there are actually two kinds of adapters: Object adapters and Class adapters.

 Object Adapter:


Class Adapter:
- Can only be use in multiple inheritance languages
- The only difference is that with class adapter we subclass the Target and the Adaptee, while with object adapter we use composition to pass request to an Adaptee.


Code Example






 

Bullet Points

  •  When you need to use an existing class and its interface is not the one you need, use an adapter.
  • An adapter changes an interface into one the client expects.
  • Implementing an adapter may require little work or a great deal of work depending on the size of the target interface.
  • There are two forms of the Adapter Patter: Object and class. Class adapters require multiple inheritance.
  • An adapter wraps an object to change its interface.  a decorator wraps an object to add new behaviors and responsibilities and a facade wraps a set of objects to simplify.

 

No comments:

Post a Comment