. PHP Design Patterns . PHP Design Patterns Proxy

PHP Design Patterns Proxy

About the Proxy

In the proxy pattern one class stands in for and handles all access to another class.

This can be because the real subject is in a different location (server, platform, etc), the real subject is cpu or memory intensive to create and is only created if necessary, or to control access to the real subject. [Design Patterns by Gamma et al p. 208 - 209] A proxy can also be used to add additional access functionality, such as recording the number of times the real subject is actually called.

In this example, the ProxyBookList is created in place of the more resource intensive BookList. ProxyBookList will only instantiate BookList the first time a method in BookList is called.

ProxyBookList.php


//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  include_once('Book.php');
  include_once('BookList.php');

  class ProxyBookList {
      
    private $bookList = NULL; 
    
    //bookList is not instantiated at construct time
    function __construct() {
    }

    function getBookCount() {
      if (NULL == $this->bookList) {
        $this->makeBookList(); 
      }
      return $this->bookList->getBookCount();
    }

    function addBook($book) {
      if (NULL == $this->bookList) {
        $this->makeBookList(); 
      }
      return $this->bookList->addBook($book);
    }  

    function getBook($bookNum) {
      if (NULL == $this->bookList) {
        $this->makeBookList();
      } 
      return $this->bookList->getBook($bookNum);
    }

    function removeBook($book) {
      if (NULL == $this->bookList) {
        $this->makeBookList();
      } 
      return $this->bookList->removeBook($book);
    }
    
    //Create 
    function makeBookList() {
      $this->bookList = new bookList();
    }

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

BookList.php


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

  include_once('Book.php');

  class BookList {
	
    private $books = array();
	private $bookCount = 0;

    public function __construct() {
    }

    public function getBookCount() {
      return $this->bookCount;
    }

    private function setBookCount($newCount) {
      $this->bookCount = $newCount;
    }

    public function getBook($bookNumberToGet) {
	  if ( (is_numeric($bookNumberToGet)) && 
           ($bookNumberToGet <= $this->getBookCount())) {
           return $this->books[$bookNumberToGet];
         } else {
           return NULL;
         }
	}

    public function addBook(Book $book_in) {
      $this->setBookCount($this->getBookCount() + 1);
      $this->books[$this->getBookCount()] = $book_in;
      return $this->getBookCount();
    }

    public function removeBook(Book $book_in) {
      $counter = 0;
      while (++$counter <= $this->getBookCount()) {
        if ($book_in->getAuthorAndTitle() == 
          $this->books[$counter]->getAuthorAndTitle())
		  {
            for ($x = $counter; $x < $this->getBookCount(); $x++) {
              $this->books[$x] = $this->books[$x + 1];
          }
          $this->setBookCount($this->getBookCount() - 1);
        }
      }
      return $this->getBookCount();
    }

  }

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

Book.php


//copyright Lawrence Truett and FluffyCat.com 2005, all rights reserved
  
  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();
    }

  }


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

testProxy.php


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

  include_once('ProxyBookList.php');
  include_once('Book.php');

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

  echo 'BEGIN TESTING PROXY PATTERN'.BR;
  echo BR;
  
  $proxyBookList = new ProxyBookList();
  
  $inBook = new Book('PHP for Cats','Larry Truett');
  
  $proxyBookList->addBook($inBook);
  
  echo 'test 1 - show the book count after a book is added'.BR;
  echo $proxyBookList->getBookCount();
  echo BR.BR;
    
  echo 'test 2 - show the book'.BR;
  $outBook = $proxyBookList->getBook(1);
  echo $outBook->getAuthorAndTitle(); 
  echo BR.BR;
  
  $proxyBookList->removeBook($outBook);
  
  echo 'test 3 - show the book count after a book is removed'.BR;
  echo $proxyBookList->getBookCount();
  echo BR.BR;

  echo 'END TESTING PROXY PATTERN'.BR;

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

output of testProxy.php

BEGIN TESTING PROXY PATTERN

test 1 - show the book count after a book is added 1

test 2 - show the book PHP for Cats by Larry Truett

test 3 - show the book count after a book is removed 0

END TESTING PROXY 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
Sign In
to add the first comment for PHP Design Patterns Proxy.