Patterns For PHP
Index Patterns News Forums

Facade

From Patterns For PHP

Contents

Introduction

At some point in PHP development, programmers will discover a need to:

  • make a software library easier to use and understand, since the facade has convenient methods for common tasks;
  • make code that uses the library more readable, for the same reason;
  • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
  • wrap a poorly designed collection of APIs with a single well-designed API (As per task needs).

In the Facade Pattern a class hides a complex subsystem from a calling class. In turn, the complex subsystem will know nothing of the calling class.

Definition

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. This can be used to simplify a number of complicated object interactions into a single interface.


Example

In this example, the CaseReverseFacade class will call a subsystem to reverse the case of a string passed from the Book class. The subsystem is controlled by the reverseCase function in the CaseReverseFacade, which in turn calls functions in the ArrayCaseReverse and ArrayStringFunctions classes. As written, the CaseReverseFacade can reverse the case of any string, but it could easily be changed to only reverse a single element of a single class.

In my example I make all elements of the Facade and the subsystem static. This could also easily be changed.


Book.php

php
class Book {
 
    private $author;
    private $title;
 
    function __construct($title_in, $author_in) {
      $this->author = $author_in;
      $this->title  = $title_in;
    }
 
    function getAuthor() {return $this->author;}
 
    function getTitle() {return $this->title;}
 
    function getAuthorAndTitle() {
      return $this->getTitle() . ' by ' . $this->getAuthor();
    }
 
  }


CaseReverseFacade.php

php
class CaseReverseFacade {
 
    public static function reverseStringCase($stringIn) {
 
      $arrayFromString = 
	    ArrayStringFunctions::stringToArray($stringIn);
 
      $reversedCaseArray = 
	    ArrayCaseReverse::reverseCase($arrayFromString);
 
      $reversedCaseString = 
	    ArrayStringFunctions::arrayToString($reversedCaseArray);
 
	  return $reversedCaseString;
 
    }
 
  }


ArrayCaseReverse.php

php
class ArrayCaseReverse {
 
	private static $uppercase_array = 
	  array('A', 'B', 'C', 'D', 'E', 'F',
	        'G', 'H', 'I', 'J', 'K', 'L',
	        'M', 'N', 'O', 'P', 'Q', 'R',
	        'S', 'T', 'U', 'V', 'W', 'X',
	        'Y', 'Z');
 
	private static $lowercase_array = 
	  array('a', 'b', 'c', 'd', 'e', 'f',
	        'g', 'h', 'i', 'j', 'k', 'l',
	        'm', 'n', 'o', 'p', 'q', 'r',
	        's', 't', 'u', 'v', 'w', 'x',
	        'y', 'z');
 
    public static function reverseCase($arrayIn) {
      $array_out = array();
	  
	  for ($x = 0; $x < count($arrayIn); $x++) {
	    if (in_array($arrayIn[$x], self::$uppercase_array)) {
          $key = array_search($arrayIn[$x], self::$uppercase_array);
		  $array_out[$x] = self::$lowercase_array[$key];
	    } elseif (in_array($arrayIn[$x], self::$lowercase_array)) {
          $key = array_search($arrayIn[$x], self::$lowercase_array);
		  $array_out[$x] = self::$uppercase_array[$key];
		} else {
		  $array_out[$x] = $arrayIn[$x];
		}
	  }
	  return $array_out;
    }
 
  }



ArrayStringFunctions.php


php
class ArrayStringFunctions {
  
    public static function arrayToString($arrayIn) {
      $string_out = NULL;
	  foreach ($arrayIn as $oneChar) {
	    $string_out .= $oneChar;
	  }
	  return $string_out;
    }
	
    public static function stringToArray($stringIn) {
      return str_split($stringIn);
    }
 
  }


testFacade.php


php
include_once('ArrayCaseReverse.php');  
  include_once('ArrayStringFunctions.php');
  include_once('Book.php');  
  include_once('CaseReverseFacade.php');
 
  echo tagins("html");
  echo tagins("head");  
  echo tagins("/head");  
  echo tagins("body");
 
  echo "BEGIN TESTING FACADE PATTERN";
  echo tagins("br").tagins("br");
  
  $book = 
    new Book("Design Patterns",
	            "Gamma, Helm, Johnson, and Vlissides");
 
  echo "Original book title: ".$book->getTitle();
  echo tagins("br").tagins("br");
 
  $bookTitleReversed = 
    CaseReverseFacade::reverseStringCase($book->getTitle());  
  
  echo "Reversed book title: ".$bookTitleReversed;
  echo tagins("br").tagins("br");
 
  echo "END TESTING FACADE PATTERN";
  echo tagins("br");
 
  echo tagins("/body");
  echo tagins("/html");
 
  //doing this so code can be displayed without breaks
  function tagins($stuffing) {
    return "<".$stuffing.">";
  }



output of testFacade.php


php
BEGIN TESTING FACADE PATTERN
 
 
Original book title: Design Patterns
 
 
Reversed book title: dESIGNpATTERNS
 
 
END TESTING FACADE 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

MediaWiki
GNU Free Documentation License 1.2