Skip to content

Commit 2d75c59

Browse files
committed
chore: translates composite pattern
1 parent 8c5276b commit 2d75c59

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ trasparente per il client. Questo è un classico caso di compromesso. A volte fa
120120
che sembra violare il principio. In alcuni casi, tuttavia, questa è una questione di prospettiva; per esempio, potrebbe
121121
sembrare sbagliato avere operazioni di gestione figli nei nodi foglia (come _add()_, _remove()_ e _getChild()_), ma poi
122122
puoi sempre cambiare prospettiva e vedere una foglia come un nodo con zero figli.
123+
124+
Composite pattern allows you to compose objects in tree structures to represent entire hierarchies. Composite pattern
125+
allows clients to treat single objects and object compositions uniformly. Using a structure
126+
composite, we can apply the same operations to both composite data and individual objects. In other words, in the
127+
most of the cases we can ignore the differences between the compositions of objects and individual objects. With this
128+
pattern it seems that the principle of single responsibility is violated as a class that implements it is found
129+
to do two things, manage a hierarchy and manage operations on the end nodes (the leaves of the tree). We can however
130+
to say that the Composite pattern takes the design of the SRP and mistakes it for _transparency_; allowing the interface
131+
Component to contain child management operations and leaf operations, a client can treat both uniformly
132+
the composite data that the leaf nodes; therefore, if an element is a composite or leaf node it becomes transparent to the client.
133+
By allowing the Component interface to contain child management operations and leaf operations, a client can
134+
treat both composites and leaf nodes uniformly; so if an element is a composite or leaf node it becomes
135+
transparent to the client. This is a classic case of compromise. Sometimes we intentionally do things one way
136+
which seems to violate the principle. In some cases, however, this is a matter of perspective; for example, it might
137+
seem wrong to have child management operations in leaf nodes (like _add () _, _remove () _ and _getChild () _), but then
138+
you can always change perspective and see a leaf as a node with zero children.
139+
123140
```
124141
php src/Composite/Menu/client.php
125142
```

src/Composite/Menu/MenuComponent.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,50 @@
44

55
namespace DesignPatterns\Composite\Menu;
66

7+
use LogicException;
8+
79
abstract class MenuComponent
810
{
911
// Composite methods
1012
public function add(MenuComponent $menuComponent): void
1113
{
12-
throw new \LogicException();
14+
throw new LogicException();
1315
}
1416

1517
public function remove(MenuComponent $menuComponent): void
1618
{
17-
throw new \LogicException();
19+
throw new LogicException();
1820
}
1921

2022
public function getChild(int $i): MenuComponent
2123
{
22-
throw new \LogicException();
24+
throw new LogicException();
2325
}
2426

25-
// Operation methods. Usati dagli oggetti MenuItems
27+
// Operation methods. Used by MenuItems objects
2628
public function getName(): string
2729
{
28-
throw new \LogicException();
30+
throw new LogicException();
2931
}
3032

3133
public function getDescription(): string
3234
{
33-
throw new \LogicException();
35+
throw new LogicException();
3436
}
3537

3638
public function getPrice(): float
3739
{
38-
throw new \LogicException();
40+
throw new LogicException();
3941
}
4042

4143
public function isVegetarian(): bool
4244
{
43-
throw new \LogicException();
45+
throw new LogicException();
4446
}
4547

46-
// Operation method usato sia dagli oggetti Menu, sia dagli oggetti MenuItems
48+
// Operation method used by both Menu objects and MenuItems objects
4749
public function print(): void
4850
{
49-
throw new \LogicException();
51+
throw new LogicException();
5052
}
5153
}

src/Composite/Menu/MenuItem.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class MenuItem extends MenuComponent
88
{
9-
private $name;
10-
private $description;
11-
private $vegetarian;
12-
private $price;
9+
private string $name;
10+
private string $description;
11+
private bool $vegetarian;
12+
private float $price;
1313

1414
public function __construct(string $name, string $description, bool $vegetarian, float $price)
1515
{

0 commit comments

Comments
 (0)