Skip to content

Instantly share code, notes, and snippets.

@ricardoaugusto
Last active May 10, 2017 17:14
Show Gist options
  • Save ricardoaugusto/cb956da41299820f9015c564600fae3d to your computer and use it in GitHub Desktop.
Save ricardoaugusto/cb956da41299820f9015c564600fae3d to your computer and use it in GitHub Desktop.
Builder Design Pattern
abstract class AbstractPageBuilder {
abstract function getPage();
}
abstract class AbstractPageDirector {
abstract function __construct(AbstractPageBuilder $builder_in);
abstract function buildPage();
abstract function getPage();
}
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>';
}
}
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;
}
}
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();
}
}
writeln('BEGIN TESTING BUILDER PATTERN');
writeln('');
$pageBuilder = new HTMLPageBuilder();
$pageDirector = new HTMLPageDirector($pageBuilder);
$pageDirector->buildPage();
$page = $pageDirector->GetPage();
writeln($page->showPage());
writeln('');
writeln('END TESTING BUILDER PATTERN');
function writeln($line_in) {
echo $line_in."<br/>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment