Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70b84b8

Browse files
committed
Builder
1 parent f142283 commit 70b84b8

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

‎Builder/index.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
/**
4+
* Design pattern "Builder" (Creational)
5+
* This is demo code
6+
* See for details: http://maxsite.org/page/php-patterns
7+
*/
8+
9+
/**
10+
* Sample Product
11+
*/
12+
class Product
13+
{
14+
private $list;
15+
16+
public function add($elem)
17+
{
18+
$this->list[] = $elem;
19+
}
20+
21+
public function getProduct()
22+
{
23+
foreach ($this->list as $el) {
24+
echo $el . '<br>';
25+
}
26+
}
27+
}
28+
29+
/**
30+
* Director use builder
31+
*/
32+
class Director
33+
{
34+
public function setConstruct($builder)
35+
{
36+
$builder->buildPartA();
37+
$builder->buildPartB();
38+
}
39+
}
40+
41+
/**
42+
* base for Builder
43+
*/
44+
abstract class BuilderAbstract
45+
{
46+
public function buildPartA()
47+
{
48+
}
49+
50+
public function buildPartB()
51+
{
52+
}
53+
54+
abstract public function getResult();
55+
}
56+
57+
/**
58+
* Builder1 knows how to make a product
59+
*/
60+
class ConcreteBuilder1 extends BuilderAbstract
61+
{
62+
private $product;
63+
64+
public function __construct()
65+
{
66+
$this->product = new Product();
67+
}
68+
69+
public function buildPartA()
70+
{
71+
$this->product->add('Builder First Part A');
72+
}
73+
74+
public function buildPartB()
75+
{
76+
$this->product->add('Builder First Part B');
77+
}
78+
79+
public function getResult()
80+
{
81+
return $this->product;
82+
}
83+
}
84+
85+
/**
86+
* Builder2 knows how to make a product
87+
*/
88+
class ConcreteBuilder2 extends BuilderAbstract
89+
{
90+
private $product;
91+
92+
public function __construct()
93+
{
94+
$this->product = new Product();
95+
}
96+
97+
public function buildPartA()
98+
{
99+
$this->product->add('Builder Second Part A');
100+
}
101+
102+
public function buildPartB()
103+
{
104+
$this->product->add('Builder Second Part B');
105+
}
106+
107+
public function getResult()
108+
{
109+
return $this->product;
110+
}
111+
}
112+
113+
114+
/**
115+
* demo
116+
*/
117+
118+
/**
119+
* set director
120+
*/
121+
$director = new Director();
122+
123+
/**
124+
* set builders
125+
*/
126+
$builder1 = new ConcreteBuilder1();
127+
$builder2 = new ConcreteBuilder2();
128+
129+
/**
130+
* set Construct
131+
*/
132+
$director->setConstruct($builder1);
133+
134+
/**
135+
* get product
136+
*/
137+
$product1 = $builder1->getResult();
138+
$product1->getProduct();
139+
/*
140+
Builder First Part A
141+
Builder First Part B
142+
*/
143+
144+
/**
145+
* use builder2 for other product2
146+
*/
147+
$director->setConstruct($builder2);
148+
$product2 = $builder2->getResult();
149+
$product2->getProduct();
150+
/*
151+
Builder Second Part A
152+
Builder Second Part B
153+
*/
154+
155+
156+
# end of file

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ See details on http://maxsite.org/page/php-patterns
1010
* "Multiton" (Creational)
1111
* "Registry" (Structural)
1212
* "Composite" (Structural)
13+
* "Builder" (Creational)
1314

1415

1516
(c) MaxSite.org, 2019, http://maxsite.org/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /