Bridge pattern in PHP

The bridge pattern is another structural design pattern. The design pattern states that the bridge pattern should decouple an abstraction from its implementation. That sounds a lot one of the main goals of OOP, programming to an interface using polymorphism and abstraction to achieve inversion of control.

As much as that’s what it sounds like, that’s not the goal of the bridge pattern, it’s more the means of achieving the goal. I like to think of the bridge pattern as a multi-dimensional strategy pattern. I’ve not covered the strategy pattern yet but you can think of it as providing an interface to build multiple strategies or algorithms which can be selected at run time.

The bridge pattern differs from this, in that you build two collections of strategies, where all strategies from one interface can make use of the strategies from the other interface. As an example lets assume you have a device strategy with 3 implementations (Phone, Tablet & Laptop), you also have 3 messaging strategies (Skype, WhatsApp, Facebook). The goal is to build an application where every device can interchangeably use every messaging platform using composition.

Why would you use the bridge pattern

The build pattern helps to reduce the number of classes required and prevents repetition of code between classes, keeping your code DRY. Also just like in the strategy pattern it allows you to change to functionality of you application at run time.

Example

interface DeviceInterface { 
    public function setSender(MessagingInterface $sender);
 
    public function send($body);
}

abstract class Device implements DeviceInterface {
    protected $sender;

    public function setSender(MessagingInterface $sender) {
        $this->sender = $sender;
    }
}

class Phone extends Device {
    public function send($body) {
        $body .= "\n\n Sent from a phone.";

        return $this->sender->send($body);
    }
}

class Tablet extends Device {
    public function send($body) {
        $body .= "\n\n Sent from a Tablet.";

        return $this->sender->send($body);
    }
}

interface MessagingInterface { 
    public function send($body);
}

class Skype implements MessagingInterface {
    public function send($body) {
        // Send a message through the Skype API
    }
}

class Whatsapp implements MessagingInterface {
    public function send($body) {
        // Send a message through the Whatsapp API
    }
}

$phone = new Phone;
$phone->setSender(new Skype);

$phone->send("Hey Buddy");

Conclusion

As we can see from the above example we have a collection of devices which all implement the same interface and we have a collection of messaging services which all implement the same interface. When building a device object, we must provide it with a messaging object, allowing us to send messages through that services API.

14 Love This

7 Comments Bridge pattern in PHP

  1. Andrey

    The bridge pattern is often confused with the adapter pattern . In fact, the bridge pattern is often implemented using the class adapter pattern , e.g. in the Java code below.

    Reply
    1. Simon

      Hi Andrey, it’s true, a lot of the structural patterns get confused with one another. This largely comes down to them all trying to achieve the same goal – identifying relationships.

      Reply

Leave a Reply

Your email address will not be published.