2011년 1월 13일 목요일

PHP Design Patterns Builder

[출처] http://www.fluffycat.com/PHP-Design-Patterns/ , http://www.liveware.kr

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();

}





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();

}




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 .= '[h1]'.$this->page_heading.'[/h1]';
$this->page .= $this->page_text;
$this->page .= '[/body]';
$this->page .= '[/html]';
}

}





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;
}

}




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();
}

}




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;




output of testBuilder.php
BEGIN TESTING BUILDER PATTERN

Testing the HTMLPage

Testing the HTMLPage

Testing, testing, testing!
Testing, testing, testing, or!
Testing, testing, testing, more!


END TESTING BUILDER PATTERN

0 개의 댓글:

댓글 쓰기