JavaScript Design Patterns - Structural - Bridge

The bridge pattern allows one interface in our class to build different implementations depending on what instance we are receiving and what instance we need to return. In the example below, we create a bridge between types of Soldiers and types of Weapons. In this way, we can correctly pass the instance of weapons to our soldiers. class Soldier { constructor(weapon) { this.weapon = weapon; } } class SuperSoldier extends Soldier { constructor(weapon) { super(weapon); } attack() { return 'SuperSoldier, Weapon: ' + this.weapon.get(); } } class IronMan extends Soldier { constructor(weapon) { super(weapon); } attack() { return 'Ironman, Weapon: ' + this.weapon.get(); } } class Weapon { constructor(type) { this.type = type; } get() { return this.type; } } class Shield extends Weapon { constructor() { super('shield'); } } class Rocket extends Weapon { constructor() { super('rocket'); } } export { SuperSoldier, IronMan, Shield, Rocket }; ???? Use this pattern when we need to use a specific implementation in runtime from an abstraction. ???? A complete example here: https://stackblitz.com/edit/vitejs-vite-efyrjy?file=main.js I hope you found it useful. Thanks for reading. ???? Let's get connected! You can find me on: Medium: https://medium.com/@nhannguyendevjs/ Dev: https://dev.to/nhannguyendevjs/ Hashnode: https://nhannguyen.hashnode.dev/ Linkedin: https://www.linkedin.com/in/nhannguyendevjs/ X (formerly Twitter): https://twitter.com/nhannguyendevjs/ Buy Me a Coffee: https://www.buymeacoffee.com/nhannguyendevjs

Mar 25, 2024 - 12:30
 0
JavaScript Design Patterns - Structural - Bridge

Image description

The bridge pattern allows one interface in our class to build different implementations depending on what instance we are receiving and what instance we need to return.

In the example below, we create a bridge between types of Soldiers and types of Weapons. In this way, we can correctly pass the instance of weapons to our soldiers.

class Soldier {
  constructor(weapon) {
    this.weapon = weapon;
  }
}

class SuperSoldier extends Soldier {
  constructor(weapon) {
    super(weapon);
  }

  attack() {
    return 'SuperSoldier, Weapon: ' + this.weapon.get();
  }
}

class IronMan extends Soldier {
  constructor(weapon) {
    super(weapon);
  }

  attack() {
    return 'Ironman, Weapon: ' + this.weapon.get();
  }
}

class Weapon {
  constructor(type) {
    this.type = type;
  }

  get() {
    return this.type;
  }
}

class Shield extends Weapon {
  constructor() {
    super('shield');
  }
}

class Rocket extends Weapon {
  constructor() {
    super('rocket');
  }
}

export { SuperSoldier, IronMan, Shield, Rocket };

???? Use this pattern when we need to use a specific implementation in runtime from an abstraction.

???? A complete example here: https://stackblitz.com/edit/vitejs-vite-efyrjy?file=main.js

I hope you found it useful. Thanks for reading. ????

Let's get connected! You can find me on:

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow

Viral News Code whisperer by profession, narrative alchemist by passion. With 6 years of tech expertise under my belt, I bring a unique blend of logic and imagination to ViralNews360. Expect everything from tech explainers that melt your brain (but not your circuits) to heartwarming tales that tug at your heartstrings. Come on in, the virtual coffee's always brewing!