Skip to main content

Bridge Design Pattern

Structural: Bridge Pattern


Could you please explain the bridge design pattern?

View Answer:
Interview Response: The Bridge pattern allows two components, a client, and a service, to work together, with each element having its interface. The Bridge Pattern is a high-level architectural pattern, and its primary goal is to write better code through two levels of abstraction. It facilitates the very loose coupling of objects and sometimes referred to as a double Adapter pattern.

Diagram:

Code Example #1:




'use strict';

class Abstraction {
constructor() {}

Operation() {
this.imp.OperationImp();
}
}

class RefinedAbstraction extends Abstraction {
constructor() {
super();
console.log('RefinedAbstraction created');
}

setImp(imp) {
this.imp = imp;
}
}

class Implementor {
constructor() {}

OperationImp() {}
}

class ConcreteImplementorA extends Implementor {
constructor() {
super();
console.log('ConcreteImplementorA created');
}

OperationImp() {
console.log('ConcreteImplementorA OperationImp');
}
}

class ConcreteImplementorB extends Implementor {
constructor() {
super();
console.log('ConcreteImplementorB created');
}

OperationImp() {
console.log('ConcreteImplementorB OperationImp');
}
}

function run() {
var abstraction = new RefinedAbstraction();
var state = Math.floor(Math.random() * 2);
if (state) abstraction.setImp(new ConcreteImplementorA());
else abstraction.setImp(new ConcreteImplementorB());

abstraction.Operation();
}

run();

/*

output:

RefinedAbstraction created
ConcreteImplementorB created
ConcreteImplementorB OperationImp

*/

Code Example #2:



This pattern's objects are as follows

Client -- Example code: the run() function

  • To request an operation, we make a call into abstraction.

Abstraction -- not used in JavaScript

  • declares a first-level abstraction interface
  • manages a reference/pointer to the Implementor

RefinedAbstraction -- Example code: Gestures, Mouse

  • implements and expands on the abstraction-defined interface

Implementor -- not used in JavaScript

  • declares an interface for second-level or implementor abstraction

ConcreteImplementor -- In example code: Screen, Audio

  • implements the Implementor interface and defines its effects

// input devices
let Gestures = function (output) {
this.output = output;

this.tap = function () {
this.output.click();
};
this.swipe = function () {
this.output.move();
};
this.pan = function () {
this.output.drag();
};
this.pinch = function () {
this.output.zoom();
};
};

let Mouse = function (output) {
this.output = output;

this.click = function () {
this.output.click();
};
this.move = function () {
this.output.move();
};
this.down = function () {
this.output.drag();
};
this.wheel = function () {
this.output.zoom();
};
};

// output devices

let Screen = function () {
this.click = function () {
console.log('Screen select');
};
this.move = function () {
console.log('Screen move');
};
this.drag = function () {
console.log('Screen drag');
};
this.zoom = function () {
console.log('Screen zoom in');
};
};

let Audio = function () {
this.click = function () {
console.log('Sound oink');
};
this.move = function () {
console.log('Sound waves');
};
this.drag = function () {
console.log('Sound screetch');
};
this.zoom = function () {
console.log('Sound volume up');
};
};

function run() {
let screen = new Screen();
let audio = new Audio();

let hand = new Gestures(screen);
let mouse = new Mouse(audio);

hand.tap();
hand.swipe();
hand.pinch();

mouse.click();
mouse.move();
mouse.wheel();
}

run();

/*

OUTPUT:

Screen select
Screen move
Screen zoom in
Sound oink
Sound waves
Sound volume up

*/

The Bridge pattern belongs to which design pattern family?

View Answer:
Interview Response: In software engineering, we define the bridge pattern as a structural design pattern.

What is the use case for the Bridge Pattern in JavaScript?

View Answer:
Interview Response: The Bridge pattern is an excellent pattern for driver development but rarely seen in JavaScript.

What are the objects that participate in the Bridge Pattern?

View Answer:
Interview Response: The objects that participate in the Bridge Pattern include the Client, Abstraction, Refined Abstraction, Implementor, and ConcreteImplementor.

Technical Response: The Bridge Pattern objects include the Client, Abstraction, Refined Abstraction, Implementor, and ConcreteImplementor.

  • Client – To request an operation, the Client makes a call to the abstraction.
  • Abstraction – The Abstraction, which is uncommon in JavaScript, declares an interface for the first level of abstraction and keeps a reference to the Implementor.
  • RefinedAbstraction – The RefinedAbstraction implements and extends the abstraction-defined interface.
  • Implementor – The Implementor interface provides an interface for the second level of implementor abstraction.
  • ConcreteImplementor – The ConcreteImplementor is responsible for implementing the Implementor interface and defining its effects.
note

JavaScript does not support abstract classes, and as a result, Abstraction and Implementor get excluded.



What are some of the advantages of employing the Bridge pattern?

View Answer:
Interview Response: Benefits of the Bridge Pattern

  • Loosely coupled code - Because the bridge pattern decouples an abstraction from its implementation, changes to the implementation do not affect the client code. The client code does not need to be compiled when the implementation changes.
  • Reduces code duplication while increasing code maintainability and reusability.
  • Classes and applications that are platform agnostic can be created.
  • Helps to promote the Open-Closed principle, new abstractions and implementations can be developed independently.
  • Decoupling abstraction from implementation: bridge pattern can avoid the binding between abstraction and implementation and select the implementation at run time.
  • Improved Extensibility – Abstraction and implementation can be extended independently.


What are some of the disadvantages of employing the Bridge pattern?

View Answer:
Interview Response: Drawbacks of the Bridge Pattern

  • Bridge pattern implementation increases code complexity.
  • Interfaces with only a single implementation.
  • Using the technique in a highly cohesive class may cause the code to become more complicated.