. PHP Design Patterns . PHP Design Patterns Builder
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();
}
download source, use right-click and "Save Target As..." to save with a .php extension.
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();
}
download source, use right-click and "Save Target As..." to save with a .php extension.
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]'; }
}
download source, use right-click and "Save Target As..." to save with a .php extension.
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; }
}
download source, use right-click and "Save Target As..." to save with a .php extension.
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(); }
}
download source, use right-click and "Save Target As..." to save with a .php extension.
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;
download source, use right-click and "Save Target As..." to save with a .php extension.
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
| Comments Comments are left by visitors to FluffyCat.com, are not endorsed by FluffyCat.com, and may or may not be accurate. |
| Comment by mat.hew Rate this Comment |
BTW a good idea Larry.
|
| Comment by mat.hew Rate this Comment |
I'm not sure, but I think, the include of HTMLPage.php in testBuilder.php is some misleading.
|
| Comment by Larry Rate this Comment |
I did this example with formatPage() in HTMLPage. I'm not completely sure if this belongs there or in HTMLPageBuilder.
|
| Sign In |
| to add your own comment |