Bridge

Intent

Decouple an abstraction from its implementation so that the two can vary independently.

Class diagram of the original GoF pattern

structure-GoF:bridge

Improvement in MixJuice

Solved Problem(s)

Extensibility problem
It is impossible to add new operations to Abstraction and Implementor without modifying the source code. (This problem is not mentioned in the GoF book.)

Used programming technique(s)

Abstract method addition to Abstraction and Implementor.

Consequences

Structure and pseudo code

structure:bridge_sample1

module original {
  define abstract class Abstraction {
    Implementor imp;
    define void operation() { imp.operationImp(); }
  }

  define class RefinedAbstraction extends Abstraction {
    ...
  }

  define abstract class Implementor {
    define abstract void operationImp();
  }

  define class ConcreteImplementorA extends Implementor {
    void operationImp() { ... }
  }

  define class ConcreteImplementorB extends Implementor {
    void operationImp() { ... }
  }
}

module extension extends original {
  class Abstraction {
    define void operation2() { imp.operationImp2(); }
  }

  class Implementor {
    define abstract void operationImp2();
  }

  class ConcreteImplementorA {
    void operationImp2() { ... }
  }

  class ConcreteImplementorB {
    void operationImp2() { ... }
  }
}

Another solution in MixJuice

Solved Problem(s)

Type safety problem
The original GoF pattern requires downcast from Implementor to a ConcreteImplementor in order to invoke the specific methods of the ConcreteImplementor. (This problem is not mentioned in the GoF book.)
Complexity problem
The class structure of the original GoF pattern is complicated. (This problem is not mentioned in the GoF book.)

Used programming technique(s)

Implementation selection of Abstraction.

Consequences

+ Downcasts are no longer needed. - This solution is applicable only if just one implementation of Abstraction is used in a program.

Structure and pseudo code

structure:bridge_sample2

module original {
  define class Abstraction {
    define void operation() {
      ... implementationOperation1(); ...
    }

    define abstract void implementationOperation1();
    define abstract void implementationOperation2();
  }

  define class RefinedAbstractionA extends Abstraction {...}
  define class RefinedAbstractionB extends Abstraction {...}
}

module implementation1 extends original {
  class Abstraction {
    void implementationOperation1() {...}
    void implementationOperation2() {...}
  }
}

module implementation2 extends original {
  class Abstraction {
    void implementationOperation1() {...}
    void implementationOperation2() {...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top