PHP Design Patterns .

PHP Design Patterns Bridge

About the Bridge


In the Bridge Design Pattern, functionality abstraction and implementation are in separate class hierarchies.

In this example we have BridgeBook which uses either BridgeBookCapsImp or BridgeBookStarsImp. BridgeBook will assign one implementation or the other each time BridgeBook is instantiated.

The bridge pattern is helpful when you want to decouple a class from it's implementation.


BridgeBook.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  include_once('BridgeBookCapsImp.php');
  include_once('BridgeBookStarsImp.php');  
  
  abstract class BridgeBook {
      
    private $bbAuthor;
    private $bbTitle;
    private $bbImp;
    
    function __construct($author_in, $title_in, $choice_in) {
      $this->bbAuthor = $author_in;
      $this->bbTitle  = $title_in;
      if ('STARS' == $choice_in) {
        $this->bbImp = new BridgeBookStarsImp();
      } else {
        $this->bbImp = new BridgeBookCapsImp();
      }
    }    
  
    function showAuthor() {
      return $this->bbImp->showAuthor($this->bbAuthor);
    }
    
    function showTitle() {
      return $this->bbImp->showTitle($this->bbTitle);
    }
  
  }
download source, use right-click and "Save Target As..." to save with a .php extension.

BridgeBookAuthorTitle.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  include_once('BridgeBook.php');;  
  
  class BridgeBookAuthorTitle extends BridgeBook {    
  
    function showAuthorTitle() {
      return $this->showAuthor() . "'s " . $this->showTitle();
    }
  
  }
download source, use right-click and "Save Target As..." to save with a .php extension.

BridgeBookTitleAuthor.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  include_once('BridgeBook.php');;  
  
  class BridgeBookTitleAuthor extends BridgeBook {    
  
    function showTitleAuthor() {
      return $this->showTitle() . ' by ' . $this->showAuthor();
    }
  
  }
download source, use right-click and "Save Target As..." to save with a .php extension.

BridgeBookImp.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved 
  
  abstract class BridgeBookImp {    
  
    abstract function showAuthor($author);
    
    abstract function showTitle($title);

}
download source, use right-click and "Save Target As..." to save with a .php extension.

BridgeBookCapsImp.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved 
  
  include_once('BridgeBookImp.php');  
  
  class BridgeBookCapsImp extends BridgeBookImp {    
  
    function showAuthor($author_in) {
      return strtoupper($author_in); 
    }
    
    function showTitle($title_in) {
      return strtoupper($title_in); 
    }
    
  }
download source, use right-click and "Save Target As..." to save with a .php extension.

BridgeBookStarsImp.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved 
  
  include_once('BridgeBookImp.php');  
  
  class BridgeBookStarsImp extends BridgeBookImp {    
  
    function showAuthor($author_in) {
      return Str_replace(" ","*",$author_in); 
    }
    
    function showTitle($title_in) {
      return Str_replace(" ","*",$title_in); 
    }
    
  }
download source, use right-click and "Save Target As..." to save with a .php extension.


testBridge.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

include_once('BridgeBookAuthorTitle.php'); include_once('BridgeBookTitleAuthor.php');

define('BR', '<'.'BR'.'>');

echo 'BEGIN TESTING BRIDGE PATTERN'.BR; echo BR; echo 'test 1 - author title with caps'.BR; $book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','CAPS'); echo $book->showAuthorTitle(); echo BR.BR;

echo 'test 2 - author title with stars'.BR; $book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','STARS'); echo $book->showAuthorTitle(); echo BR.BR;

echo 'test 3 - title author with caps'.BR; $book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','CAPS'); echo $book->showTitleAuthor(); echo BR.BR;

echo 'test 4 - title author with stars'.BR; $book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','STARS'); echo $book->showTitleAuthor(); echo BR.BR;

echo 'END TESTING BRIDGE PATTERN'.BR;
download source, use right-click and "Save Target As..." to save with a .php extension.

output of testBridge.php

BEGIN TESTING BRIDGE PATTERN

test 1 - author title with caps LARRY TRUETT's PHP FOR CATS

test 2 - author title with stars Larry*Truett's PHP*for*Cats

test 3 - title author with caps PHP FOR CATS by LARRY TRUETT

test 4 - title author with stars PHP*for*Cats by Larry*Truett

END TESTING BRIDGE PATTERN

References


Design Patterns
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

PHP 5
The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by md2perpe on 2013-08-01 Rate this Comment

Is this really the Bridge Pattern? To me it just looks like the Strategy pattern for using caps or stars, plus subclassing for choosing in which order author and title are shown. It feels like there should be something more to a pattern than just being a combo of two other patterns.

Comment by Larry on 2013-04-03 Rate this Comment

As noted in GOF p. 161, The Bridge Pattern and the Adapter Pattern are similar in that they both fill the need to allow unrelated classes to work together.

One essential difference is that in the Bridge Pattern the classes are designed to work together and are aware of each other's existence.

 
Sign in to comment on PHP Design Patterns Bridge.