Skip to content

Commit 5db7d45

Browse files
committed
Builder design pattern + improvements in phpunit
1 parent fed3f73 commit 5db7d45

21 files changed

+583
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Abstract Factory Design Pattern
2+
3+
### Intent
4+
5+
“Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”
6+
7+
Excerpt From: Erich Gamma. “Design Patterns: Elements of Reusable Object-Oriented Software.”
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder;
4+
5+
class ClassicWatch extends Watch
6+
{
7+
8+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder;
4+
5+
/**
6+
* Class ClassicWatchBuilder
7+
* @package PhpDesignPatterns\Creational\Builder
8+
*/
9+
class ClassicWatchBuilder implements WatchBuilderInterface
10+
{
11+
/**
12+
* Instance of SportWatch.
13+
*
14+
* @var SportWatch
15+
*/
16+
private $watch;
17+
18+
/**
19+
* @return mixed
20+
*/
21+
public function createWatch()
22+
{
23+
$this->setWatch(new ClassicWatch);
24+
return $this;
25+
}
26+
27+
/**
28+
* @return mixed
29+
*/
30+
public function addBox()
31+
{
32+
$this->watch->addComponent("Watch box", new Components\Box());
33+
return $this;
34+
}
35+
36+
/**
37+
* @return mixed
38+
*/
39+
public function addHands()
40+
{
41+
$this->watch->addComponent("Seconds hand", new Components\Hand());
42+
$this->watch->addComponent("Minutes hand", new Components\Hand());
43+
$this->watch->addComponent("Hours hand", new Components\Hand());
44+
return $this;
45+
}
46+
47+
/**
48+
* @return mixed
49+
*/
50+
public function addBands()
51+
{
52+
$this->watch->addComponent("Leather upper band", new Components\Band());
53+
$this->watch->addComponent("Leather lower band", new Components\Band());
54+
return $this;
55+
}
56+
57+
/**
58+
* @return mixed
59+
*/
60+
public function addMovements()
61+
{
62+
$this->watch->addComponent("Principal watch movement", new Components\Movement());
63+
$this->watch->addComponent("Date watch movement", new Components\Movement());
64+
return $this;
65+
}
66+
67+
/**
68+
* @return mixed
69+
*/
70+
public function getWatch()
71+
{
72+
return $this->watch;
73+
}
74+
75+
/**
76+
* @param Watch $watch
77+
* @return ClassicWatchBuilder
78+
*/
79+
public function setWatch(Watch $watch)
80+
{
81+
$this->watch = $watch;
82+
return $this;
83+
}
84+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder\Components;
4+
5+
class Band implements ComponentInterface
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder\Components;
4+
5+
class Box implements ComponentInterface
6+
{
7+
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder\Components;
4+
5+
/**
6+
* Interface ComponentInterface
7+
* @package PhpDesignPatterns\Creational\Builder\Components
8+
*/
9+
interface ComponentInterface
10+
{
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder\Components;
4+
5+
class Hand implements ComponentInterface
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder\Components;
4+
5+
class Movement implements ComponentInterface
6+
{
7+
8+
}

Creational/Builder/Director.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Creational\Builder;
4+
5+
/**
6+
* Class Director
7+
* @package PhpDesignPatterns\Creational\Builder
8+
*/
9+
class Director
10+
{
11+
12+
/**
13+
* Builds a Watch using a builder.
14+
*
15+
* @param WatchBuilderInterface $builder
16+
* @return Instance of...
17+
*/
18+
public function build(WatchBuilderInterface $builder)
19+
{
20+
return $builder
21+
->createWatch()
22+
->addMovements()
23+
->addHands()
24+
->addBox()
25+
->addBands()
26+
->getWatch()
27+
;
28+
}
29+
}

Creational/Builder/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Builder Design Pattern
2+
3+
### Intent
4+
5+
“Separate the construction of a complex object from its representation so that the same construction process can create different representations.”
6+
7+
Excerpt From: Erich Gamma. “Design Patterns: Elements of Reusable Object-Oriented Software.”

0 commit comments

Comments
 (0)