Skip to content

Commit 7b2113b

Browse files
committed
Add Composite Design Pattern and README.md in some folders
1 parent cb55fb4 commit 7b2113b

File tree

15 files changed

+458
-3
lines changed

15 files changed

+458
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
2-
.idea/
2+
/.idea/
3+
/coverage/

Behavioral/README.md

Whitespace-only changes.

Creational/README.md

Whitespace-only changes.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
Running Unit Tests
88

99
* Run `composer install`
10-
* Now launch UTs running `vendor/bin/phpunit Creational/`
10+
* Now launch UTs running `vendor/bin/phpunit Tests/`

Structural/Composite/Phone.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Structural\Composite;
4+
5+
/**
6+
* Class Phone (Composite object).
7+
* @package PhpDesignPatterns\Structural\Composite
8+
*/
9+
class Phone extends PhoneElement
10+
{
11+
/**
12+
* List of PhoneElement.
13+
*
14+
* @var array of PhoneElement
15+
*/
16+
private $elements;
17+
18+
/**
19+
* Build a phone.
20+
*
21+
* @return string
22+
*/
23+
public function build()
24+
{
25+
$phone = "";
26+
if(empty($this->elements)) {
27+
return $phone;
28+
}
29+
30+
foreach($this->elements as $element)
31+
{
32+
$phone .= $element->build();
33+
}
34+
return $phone;
35+
}
36+
37+
/**
38+
* Add a PhoneElement child.
39+
*
40+
* @param PhoneElement $element
41+
* @return PhoneDisplay
42+
*/
43+
public function add(PhoneElement $element)
44+
{
45+
$this->elements[] = $element;
46+
return $this;
47+
}
48+
49+
/**
50+
* Removes a child.
51+
*
52+
* @param mixed $child_key
53+
* @return PhoneDisplay
54+
*/
55+
public function remove($child_key)
56+
{
57+
if(!empty($this->elements[$child_key])) {
58+
unset($this->elements[$child_key]);
59+
}
60+
return $this;
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Structural\Composite;
4+
5+
/**
6+
* Class PhoneButton
7+
* @package PhpDesignPatterns\Structural\Composite
8+
*/
9+
class PhoneButton extends PhoneElement
10+
{
11+
/**
12+
* Build a phone button.
13+
*
14+
* @return string
15+
*/
16+
public function build()
17+
{
18+
return "This is a phone button.".PHP_EOL;
19+
}
20+
21+
/**
22+
* Add a PhoneElement child.
23+
*
24+
* @param PhoneElement $element
25+
* @return PhoneButton
26+
*/
27+
public function add(PhoneElement $element)
28+
{
29+
return $this;
30+
}
31+
32+
/**
33+
* Removes a child.
34+
*
35+
* @param mixed $child_key
36+
* @return PhoneButton
37+
*/
38+
public function remove($child_key)
39+
{
40+
return $this;
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Structural\Composite;
4+
5+
/**
6+
* Class PhoneDisplay
7+
* @package PhpDesignPatterns\Structural\Composite
8+
*/
9+
class PhoneDisplay extends PhoneElement
10+
{
11+
/**
12+
* Build a phone display.
13+
*
14+
* @return string
15+
*/
16+
public function build()
17+
{
18+
return "This is a phone display.".PHP_EOL;
19+
}
20+
21+
/**
22+
* Add a PhoneElement child.
23+
*
24+
* @param PhoneElement $element
25+
* @return PhoneDisplay
26+
*/
27+
public function add(PhoneElement $element)
28+
{
29+
return $this;
30+
}
31+
32+
/**
33+
* Removes a child.
34+
*
35+
* @param mixed $child_key
36+
* @return PhoneDisplay
37+
*/
38+
public function remove($child_key)
39+
{
40+
return $this;
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Structural\Composite;
4+
5+
/**
6+
* Class PhoneElement
7+
* @package PhpDesignPatterns\Structural\Composite
8+
*/
9+
abstract class PhoneElement
10+
{
11+
/**
12+
* Builds an element.
13+
* @return string
14+
*/
15+
abstract public function build();
16+
17+
/**
18+
* @param PhoneElement $element Element to add.
19+
* @return PhoneElement
20+
*/
21+
abstract public function add(PhoneElement $element);
22+
23+
/**
24+
* Remove a PhoneElement child.
25+
*
26+
* @param mixed $child_key
27+
* @return PhoneElement
28+
*/
29+
abstract public function remove($child_key);
30+
}

Structural/Composite/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Composite Design Pattern
2+
3+
### Intent
4+
5+
“Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.”
6+
7+
Excerpt From: Erich Gamma. “Design Patterns: Elements of Reusable Object-Oriented Software.”
8+

Structural/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)