DEV Community

Coder
Coder

Posted on • Originally published at itscoderslife.wordpress.com on

1

Bridge Design Pattern [Structural]

Why? Increasing feature set explode class hierarchies. The Bridge pattern dissolves complex class hierarchies by separating common and specific functionality into different hierarchies.

In typical software development, as we add new features, the number of subclasses increases at an alarming rate. By decoupling an implementation from an interface, both can vary independently.

Goal is to separate out common functionality and specific ones.

The bridge pattern separates the abstraction from its implementation and reduces the impact of the changes.

Example:

Consider 2 classes – GroupChat & DirectChat. Now if these were in a plain chat format. Then you get a requirement to add support for Secure Chat and Self-destructing chat, now the general tendency is to extend each chat into 2 others SecureGroupChat and SelfDestructGroupChat also SecureDirectChat and SelfDestructDirectChat. Now consider if there is another type of chat – BroadcastChat and we need to extend this to SecureBroadcastChat and SelfDestructBroadcastChat. Now we have a total of 9 classes.

Bridge pattern – solves this with interfaces and a bridge between the interface.

Bridge.jpg

Using bridge pattern all Chat classes implement ChatInterface which will include a MessageInterface. On the other side all 3 Secure, SelfDestruct and Plain Message types will conform to MessageInterface.

Advantages:

  1. Separates Common and specific functionality
  2. Increases Cohesiveness
  3. Removes inheritance (hierarchies)
  4. Removes tight coupling
  5. Reduces the number of classes

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay