Skip to content

Commit 7132523

Browse files
authored
Merge pull request #5 from growthdev-repo/feature/create-strategy-pattern
Feature/create strategy pattern
2 parents 673fff8 + 4a78694 commit 7132523

16 files changed

+342
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ Casos você queira se aprofundar no tema, não deixe de conheceu o site Growth
1010
## Organização do Projeto:
1111

1212
- `src/`
13+
- `Behavioral/`
14+
- `Creational/`
15+
- `Structural/`
16+
- `tests/`
1317
- `Behavioral/`
1418
- `Creational/`
1519
- `Structural/`
20+
1621

1722
A estrutura da pastas de `tests/` segue a mesma estrutura
1823

@@ -36,7 +41,9 @@ Este projetos tem um arquivo `Makefile` para a execução dos testes
3641

3742
`make test`
3843

44+
5. Para executar os testes de uma classe especídica:
3945

46+
`make test NomeDaClass`
4047
## Sobre nós
4148

4249
Este projeto foi desenvoldido por Walmir Silva autor do blog [https://growthdev.com.br](https://growthdev.com.br/)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;
6+
7+
class CashPaymentMethod implements PaymentMethodStrategy
8+
{
9+
private const DISCOUNT_PERCENT = 0.10; // 10%
10+
11+
public function pay(float $amount): float
12+
{
13+
return $amount - ($amount * self::DISCOUNT_PERCENT);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;
6+
7+
class CreditCardPaymentMethod implements PaymentMethodStrategy
8+
{
9+
private const DISCOUNT_PERCENT = 0.00; // 0%
10+
11+
public function pay(float $amount): float
12+
{
13+
return $amount - ($amount * self::DISCOUNT_PERCENT);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;
6+
7+
class DebitCardPaymentMethod implements PaymentMethodStrategy
8+
{
9+
private const DISCOUNT_PERCENT = 0.05; // 5%
10+
11+
public function pay(float $amount): float
12+
{
13+
return $amount - ($amount * self::DISCOUNT_PERCENT);
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;
6+
7+
interface PaymentMethodStrategy
8+
{
9+
public function pay(float $amount): float;
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;
6+
7+
final class PaymentProcessor
8+
{
9+
private PaymentMethodStrategy $paymentMethod;
10+
private float $amount;
11+
12+
public function __construct(PaymentMethodStrategy $paymentMethod)
13+
{
14+
$this->paymentMethod = $paymentMethod;
15+
}
16+
17+
public function processPayment(float $amount): void
18+
{
19+
$this->amount = $this->paymentMethod->pay($amount);
20+
}
21+
22+
public function getAmount(): float
23+
{
24+
return $this->amount;
25+
}
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;
6+
7+
final class PaymentProcessorWithoutStrategy
8+
{
9+
public const PAYMENT_TYPE_CREDIT_CARD = 'credit_card';
10+
public const PAYMENT_TYPE_DEBIT_CARD = 'debit_card';
11+
public const PAYMENT_TYPE_CASH = 'by_cash';
12+
13+
private string $paymentMethod;
14+
private float $amount;
15+
16+
public function __construct(string $paymentMethod)
17+
{
18+
$this->paymentMethod = $paymentMethod;
19+
}
20+
21+
public function processPayment(float $amount): void
22+
{
23+
switch ($this->paymentMethod) {
24+
case self::PAYMENT_TYPE_CREDIT_CARD:
25+
$creditCard = new CreditCardPaymentMethod();
26+
$this->amount = $creditCard->pay($amount);
27+
break;
28+
case self::PAYMENT_TYPE_DEBIT_CARD:
29+
$debitCard = new DebitCardPaymentMethod();
30+
$this->amount = $debitCard->pay($amount);
31+
break;
32+
case self::PAYMENT_TYPE_CASH:
33+
$cash = new CashPaymentMethod();
34+
$this->amount = $cash->pay($amount);
35+
break;
36+
default:
37+
throw new \InvalidArgumentException('Invalid payment method');
38+
}
39+
}
40+
41+
public function getAmount(): float
42+
{
43+
return $this->amount;
44+
}
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;
6+
7+
use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\MusicalInstrumentStrategy;
8+
use Growthdev\DesignPatterns\Creational\FactoryMethod\Factory\AcousticGuitarFactory;
9+
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\AcousticGuitar;
10+
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;
11+
12+
class AcousticGuitarStrategy implements MusicalInstrumentStrategy
13+
{
14+
private string $brand;
15+
16+
public function __construct(string $brand)
17+
{
18+
$this->brand = $brand;
19+
}
20+
21+
public function build(): MusicalInstrument
22+
{
23+
return (new AcousticGuitarFactory())
24+
->createMusicalInstrument($this->brand);
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;
6+
7+
use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\MusicalInstrumentStrategy;
8+
use Growthdev\DesignPatterns\Creational\FactoryMethod\Factory\ElectricGuitarFactory;
9+
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;
10+
11+
class ElectricGuitarStrategy implements MusicalInstrumentStrategy
12+
{
13+
private string $brand;
14+
15+
public function __construct(string $brand)
16+
{
17+
$this->brand = $brand;
18+
}
19+
20+
public function build(): MusicalInstrument
21+
{
22+
return (new ElectricGuitarFactory())
23+
->createMusicalInstrument($this->brand);
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;
6+
7+
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;
8+
9+
class MusicalInstrumentFactory
10+
{
11+
public function createMusicalInstrument(
12+
MusicalInstrumentStrategy $musicalInstrument
13+
): MusicalInstrument {
14+
return $musicalInstrument->build();
15+
}
16+
}
17+
18+
19+

0 commit comments

Comments
 (0)