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);
    }
  
  }
To download source right-click here and "Save As...".

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();
    }
  
  }
To download source right-click here and "Save As...".

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();
    }
  
  }
To download source right-click here and "Save As...".

BridgeBookImp.php

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

}
To download source right-click here and "Save As...".

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); 
    }
    
  }
To download source right-click here and "Save As...".

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); 
    }
    
  }
To download source right-click here and "Save As...".


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;
To download source right-click here and "Save As...".

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 Larry on 2014-07-04 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.