Bridge

The Bridge pattern can be quite straightforward; it effectively allows us to decouple an abstraction from an implementation so the two can vary independently.

When classes vary frequently, bridging an interface and a concrete class allows developers to vary their classes with greater ease.

Let's propose a generic messenger interface that has the ability to send some form of message, Messenger.php:

<?php 
 
interface Messenger 
{ 
  public function send($body); 
} 

One specific implementation of this interface is an InstantMessenger application, InstantMessenger.php:

<?php 
 
class InstantMessenger implements Messenger 
{ 
  public function send($body) 
  { 
    echo "InstantMessenger: " . $body; 
  } 
} 

Similarly, we can do the same with an SMS application, SMS.php:

Get Mastering PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.