-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuilder.php
103 lines (92 loc) · 2.78 KB
/
builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// Problem
// An application needs to create the elements of a complex aggregate.
// The specification for the aggregate exists on secondary storage and
// one of many representations needs to be built in primary storage.
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/>";
}