Js bridge pattern

From Intense-Visions/harness-engineering · View interactive version

Js bridge pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Decouple abstraction from implementation so both can vary independently

How to use this skill

Skill file

JS Bridge Pattern

Decouple abstraction from implementation so both can vary independently

When to Use

Instructions

  1. Separate the abstraction (high-level logic) from the implementation (low-level detail).
  2. The abstraction holds a reference to an implementation object.
  3. Both can be subclassed independently — new abstractions do not require new implementations and vice versa.
  4. Inject the implementation via the constructor.
// Implementation interface
class Renderer {
  renderCircle(radius) {
    throw new Error('Not implemented');
  }
}

class SVGRenderer extends Renderer {
  renderCircle(radius) {
    return `<circle r="${radius}"/>`;
  }
}

class CanvasRenderer extends Renderer {
  renderCircle(radius) {
    return `ctx.arc(0,0,${radius},0,2*PI)`;
  }
}

// Abstraction
class Shape {
  constructor(renderer) {
    this.renderer = renderer;
  }
}

class Circle extends Shape {
  constructor(radius, renderer) {
    super(renderer);
    this.radius = radius;
  }
  draw() {
    return this.renderer.renderCircle(this.radius);
  }
}

Details

The Bridge pattern separates an abstraction from its implementation so that the two can evolve independently. Without Bridge, combining M abstractions with N implementations requires M\*N classes. With Bridge, it requires M + N.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/bridge-pattern

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.