A.Adapter Pattern

  1. Adapter Patterns

      1. Convert the interface of a class into another interface clients expect.

      2. Whole motivation is to lets classes work together that could not otherwise because of incompatible interfaces.

      3. Primary Used when it is not possible to change the client or target code to make them compatible.

  2. Examples

  1. Overview Tutorials

      1. http://www.oodesign.com/adapter-pattern.html

  2. Adapters Types

    1. Objects Adapters

        1. Objects Adapters are the classical example of the adapter pattern. It uses composition, the Adaptee delegates the calls to Adaptee

      1. Implementation

          1. Client holds an reference to instance of Abstract Target that actually holds instance of of actual Adapter that Inherit Abstract Target.

          2. Abstract Adapter further holds instance of Adaptee (Target Interface) and rout calls to it.

      2. Advantage and Limitations

          1. The main advantage is that the Adapter adapts not only the Adpatee but all its subclasses with one "small" restriction: all the subclasses which don't add new methods, because the used mechanism is delegation.

          2. So for any new method the Adapter must be changed or extended to expose the new methods as well. The main disadvantage is that it requires writing all the code for delegating the entire necessary requests tot the Adaptee.

    1. Class Adapters - Based on Multiple Inheritances

      1. It uses multiple inheritance and usually not recommended also not supported in C#

    1. Two-Ways Adapters

        1. The Two-Ways Adapters are adapters that implements both interfaces of Target and Adaptee. The adapted object can be used as Target in new systems dealing with Target classes or as Adaptee in other systems dealing with Adaptee classes.

        2. Going further on this line of thinking, we can have adapters implementing n interfaces, adapting to n systems. Two-way adapters and n-way adapters are hard to implement in systems not supporting multiple inheritance. If adapter has to extend the Target class it can not extent another class like Adaptee, so the Adaptee should be an interface and all the calls should be delegated from the adapter to the Adaptee object.

    1. Runtime Adapter pattern (An implementation of Strategy Pattern)

        1. If we are writing a code that is replacing adapters at run time then we are actually implementing strategy pattern. In this case all the adapters should be derived from a common interface.

        2. If we have several modules implementing the same functionality and we wrote adapters for them, the adapters are implementing the same interface. We can simply replace the adapters objects at run time because they implements the same interface.

  1. Specific Considerations

    1. Adapter VS Bridge

      1. Adapter is very close to Bridge pattern as discussed in next section Actually Adapter Pattern + Strategy Pattern =Bridge Pattern