BRIDGE and ADAPTER

Bridge

Implements a group of abstract classes in a hierarchy and the corresponding implementations as independent classes that can be combined dynamically. The abstraction and the implementation can vary independently.

It allows to

The abstract and implementation hierarchies can be extended independently. For example




  class Implementor
  {
    public:
      virtual void operation() = 0;
      virtual void another_operation() = 0;
  };

  class Abstraction
  {
    public:
      Abstraction( Implementor * imp ) : mImp( imp ) { } 
      
      virtual void operation() { mImp->operation(); }

    protected:
      Implementor * mImp;
  };


  class RefinedImplementor : public Implementor
  {
    public:
      virtual void second_operation() = 0;
      virtual void operation() { ... }
  };

  class RefinedAbstraction : public Abstraction
  {
    public:
      RefinedAbstraction( RefinedImplementor * imp ) : Abstraction( imp ) { }

      void second_operation() { mImp->second_operation(); }

      void another_operation() { mImp->another_operation(); }
  };

Adapter

The Adapter converts between the interface of a class (Adaptee) and the interface (Target) that the client expects. The Adapter inherits from the Target and can either inherit from the Adaptee (multiple inheritance, as in the sample code below) or delegate to the Adaptee (as in the figure).




  class Target
  {
    public:
      virtual void request() = 0;
  };

  class Adaptee
  {
    protected:
      void specific_request();
  };

  class Adapter : public Target, public Adaptee
  {
    public:
      void request() { specific_request(); }
  };

See also C# design patterns: Adapter, by S.J. Metsker, 2004.