PHP Design Patterns .

PHP Design Patterns Builder

About the Builder


In the Builder Pattern a director and a builder work together to build an object. The director controls the building and specifies what parts and variations will go into an object. The builder knows how to assemble the object given specification.

In this example we have a director, HTMLPageDirector, which is given a builder, HTMLPageBuilder. The director tells the builder what the pageTitle will be, what the pageHeading will be, and gives multiple lines of text for the page. The director then has the bulder do a final assembly of the parts, and return the page.

Note that the html tags I use in the example code have [ and ] instead of < and > so this page will display correctly.


AbstractPageBuilder.php

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

abstract class AbstractPageBuilder {

abstract function getPage();

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


AbstractPageDirector.php

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

abstract class AbstractPageDirector {

abstract function __construct(AbstractPageBuilder $builder_in);

abstract function buildPage();

abstract function getPage();

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


HTMLPage.php

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

class HTMLPage {

private $page = NULL;

private $page_title = NULL; private $page_heading = NULL; private $page_text = NULL;

function __construct() { }

function showPage() { return $this->page; }

function setTitle($title_in) { $this->page_title = $title_in; }

function setHeading($heading_in) { $this->page_heading = $heading_in; }

function setText($text_in) { $this->page_text .= $text_in; }

function formatPage() { $this->page = '[html]'; $this->page .= '[head][title]'.$this->page_title.'[/title][/head]'; $this->page .= '[body]'; $this->page .= '[h 1]'.$this->page_heading.'[/h 1]'; $this->page .= $this->page_text; $this->page .= '[/body]'; $this->page .= '[/html]'; }

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

HTMLPageBuilder.php

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

include_once('AbstractPageBuilder.php'); include_once('HTMLPage.php');

class HTMLPageBuilder extends AbstractPageBuilder {

private $page = NULL;

function __construct() { $this->page = new HTMLPage(); }

function setTitle($title_in) { $this->page->setTitle($title_in); }

function setHeading($heading_in) { $this->page->setHeading($heading_in); }

function setText($text_in) { $this->page->setText($text_in); }

function formatPage() { $this->page->formatPage(); }

function getPage() { return $this->page; }

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

HTMLPageDirector.php

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

include_once('AbstractPageBuilder.php'); include_once('AbstractPageDirector.php');

class HTMLPageDirector extends AbstractPageDirector {

private $builder = NULL;

public function __construct(AbstractPageBuilder $builder_in) { $this->builder = $builder_in; }

public function buildPage() { $this->builder->setTitle('Testing the HTMLPage'); $this->builder->setHeading('Testing the HTMLPage'); $this->builder->setText('Testing, testing, testing!'); $this->builder->setText('Testing, testing, testing, or!'); $this->builder->setText('Testing, testing, testing, more!'); $this->builder->formatPage(); }

public function getPage() { return $this->builder->getPage(); }

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


testBuilder.php

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

include_once('BookSingleton.php'); include_once('HTMLPage.php'); include_once('HTMLPageBuilder.php'); include_once('HTMLPageDirector.php');

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

echo 'BEGIN TESTING BUILDER PATTERN'.BR; echo BR;

$pageBuilder = new HTMLPageBuilder(); $pageDirector = new HTMLPageDirector($pageBuilder); $pageDirector->buildPage(); $page = $pageDirector->GetPage(); echo $page->showPage();

echo BR.BR; echo 'END TESTING BUILDER PATTERN'.BR;
To download source right-click here and "Save As...".

output of testBuilder.php

BEGIN TESTING BUILDER PATTERN

<html> <head><title>Testing the HTMLPage</title></head> <body> <h1>Testing the HTMLPage</h1> Testing, testing, testing! Testing, testing, testing, or! Testing, testing, testing, more! </body> </html>

END TESTING BUILDER 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 mat.hew on 2009-08-17 Rate this Comment

I'm not sure, but I think, the include of HTMLPage.php in testBuilder.php is some misleading.
And in the same script '$page = $pageDirector->GetPage();' is function 'getPage' to use (sorry, I'm java developer too ... case sensitive detailer).

Comment by Larry on 2009-08-17 Rate this Comment

I did this example with formatPage() in HTMLPage. I'm not completely sure if this belongs there or in HTMLPageBuilder.

I'm also not sure if the builder example needs another director/builder set - such as XML or RSS builder to really show the pattern properly.

 
Sign in to comment on PHP Design Patterns Builder.