Member-only story

What is the Bridge Design Pattern?

Avoid unnecessary complexity with the Bridge Pattern

Jakub Kapuscik
Better Programming

Photo by Modestas Urbonas on Unsplash

Sometimes important features of a class can — or even should — be developed independently. The Bridge helps us split code into smaller pieces and develop them separately. In this design pattern, we have two elements:

  • abstraction
  • implementation

Abstraction is the part that is used directly by the client. It also delegates jobs to the implementation. Imagine that we have an application that prepares HTML content for displays. Depending on the configuration of the display, we would like our content to look a bit different. All displays share the same interface:

<?php

namespace App\Structural\Bridge;

abstract readonly class Display
{
public function __construct(protected Content $content)
{
}

abstract public function render(): string;

}

Right now we have two different types. The standard one just shows the content.

<?php

namespace App\Structural\Bridge;

final readonly class Standard extends Display
{
public function render(): string
{
$html = $this->content->getHtml();
$css = $this->content->getCss();

return <<<CONTENT
$html;

<style>
$css
</style>…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

Write a response