Skip to content

Commit 8dd3959

Browse files
author
Dominik Liebler
committed
more simple an concise example of AbstractFactory
1 parent dc64f24 commit 8dd3959

14 files changed

+161
-169
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.idea
33
/nbproject
44
/vendor/
5-
_build/
5+
/_build/
66
*.mo
77
.vagrant/
88
phpunit.xml

Creational/AbstractFactory/CsvParser.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
class DigitalProduct implements Product
6+
{
7+
/**
8+
* @var int
9+
*/
10+
private $price;
11+
12+
public function __construct(int $price)
13+
{
14+
$this->price = $price;
15+
}
16+
17+
public function calculatePrice(): int
18+
{
19+
return $this->price;
20+
}
21+
}

Creational/AbstractFactory/JsonParser.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

Creational/AbstractFactory/Parser.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

Creational/AbstractFactory/ParserFactory.php

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
interface Product
6+
{
7+
public function calculatePrice(): int;
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
class ProductFactory
6+
{
7+
const SHIPPING_COSTS = 50;
8+
9+
public function createShippableProduct(int $price): Product
10+
{
11+
return new ShippableProduct($price, self::SHIPPING_COSTS);
12+
}
13+
14+
public function createDigitalProduct(int $price): Product
15+
{
16+
return new DigitalProduct($price);
17+
}
18+
}

Creational/AbstractFactory/README.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ Code
2121

2222
You can also find this code on `GitHub`_
2323

24-
Parser.php
24+
Product.php
2525

26-
.. literalinclude:: Parser.php
26+
.. literalinclude:: Product.php
2727
:language: php
2828
:linenos:
2929

30-
CsvParser.php
30+
ShippableProduct.php
3131

32-
.. literalinclude:: CsvParser.php
32+
.. literalinclude:: ShippableProduct.php
3333
:language: php
3434
:linenos:
3535

36-
JsonParser.php
36+
DigitalProduct.php
3737

38-
.. literalinclude:: JsonParser.php
38+
.. literalinclude:: DigitalProduct.php
3939
:language: php
4040
:linenos:
4141

42-
ParserFactory.php
42+
ProductFactory.php
4343

44-
.. literalinclude:: ParserFactory.php
44+
.. literalinclude:: ProductFactory.php
4545
:language: php
4646
:linenos:
4747

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
class ShippableProduct implements Product
6+
{
7+
/**
8+
* @var float
9+
*/
10+
private $productPrice;
11+
12+
/**
13+
* @var float
14+
*/
15+
private $shippingCosts;
16+
17+
public function __construct(int $productPrice, int $shippingCosts)
18+
{
19+
$this->productPrice = $productPrice;
20+
$this->shippingCosts = $shippingCosts;
21+
}
22+
23+
public function calculatePrice(): int
24+
{
25+
return $this->productPrice + $this->shippingCosts;
26+
}
27+
}

0 commit comments

Comments
 (0)