Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ Casos você queira se aprofundar no tema, não deixe de conheceu o site Growth
## Organização do Projeto:

- `src/`
- `Behavioral/`
- `Creational/`
- `Structural/`
- `tests/`
- `Behavioral/`
- `Creational/`
- `Structural/`


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

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

`make test`

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

`make test NomeDaClass`
## Sobre nós

Este projeto foi desenvoldido por Walmir Silva autor do blog [https://growthdev.com.br](https://growthdev.com.br/)
Expand Down
15 changes: 15 additions & 0 deletions src/Behavioral/Strategy/Payment/CashPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;

class CashPaymentMethod implements PaymentMethodStrategy
{
private const DISCOUNT_PERCENT = 0.10; // 10%

public function pay(float $amount): float
{
return $amount - ($amount * self::DISCOUNT_PERCENT);
}
}
15 changes: 15 additions & 0 deletions src/Behavioral/Strategy/Payment/CreditCardPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;

class CreditCardPaymentMethod implements PaymentMethodStrategy
{
private const DISCOUNT_PERCENT = 0.00; // 0%

public function pay(float $amount): float
{
return $amount - ($amount * self::DISCOUNT_PERCENT);
}
}
15 changes: 15 additions & 0 deletions src/Behavioral/Strategy/Payment/DebitCardPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;

class DebitCardPaymentMethod implements PaymentMethodStrategy
{
private const DISCOUNT_PERCENT = 0.05; // 5%

public function pay(float $amount): float
{
return $amount - ($amount * self::DISCOUNT_PERCENT);
}
}
10 changes: 10 additions & 0 deletions src/Behavioral/Strategy/Payment/PaymentMethodStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;

interface PaymentMethodStrategy
{
public function pay(float $amount): float;
}
26 changes: 26 additions & 0 deletions src/Behavioral/Strategy/Payment/PaymentProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;

final class PaymentProcessor
{
private PaymentMethodStrategy $paymentMethod;
private float $amount;

public function __construct(PaymentMethodStrategy $paymentMethod)
{
$this->paymentMethod = $paymentMethod;
}

public function processPayment(float $amount): void
{
$this->amount = $this->paymentMethod->pay($amount);
}

public function getAmount(): float
{
return $this->amount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\Payment;

final class PaymentProcessorWithoutStrategy
{
public const PAYMENT_TYPE_CREDIT_CARD = 'credit_card';
public const PAYMENT_TYPE_DEBIT_CARD = 'debit_card';
public const PAYMENT_TYPE_CASH = 'by_cash';

private string $paymentMethod;
private float $amount;

public function __construct(string $paymentMethod)
{
$this->paymentMethod = $paymentMethod;
}

public function processPayment(float $amount): void
{
switch ($this->paymentMethod) {
case self::PAYMENT_TYPE_CREDIT_CARD:
$creditCard = new CreditCardPaymentMethod();
$this->amount = $creditCard->pay($amount);
break;
case self::PAYMENT_TYPE_DEBIT_CARD:
$debitCard = new DebitCardPaymentMethod();
$this->amount = $debitCard->pay($amount);
break;
case self::PAYMENT_TYPE_CASH:
$cash = new CashPaymentMethod();
$this->amount = $cash->pay($amount);
break;
default:
throw new \InvalidArgumentException('Invalid payment method');
}
}

public function getAmount(): float
{
return $this->amount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;

use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\MusicalInstrumentStrategy;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Factory\AcousticGuitarFactory;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\AcousticGuitar;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;

class AcousticGuitarStrategy implements MusicalInstrumentStrategy
{
private string $brand;

public function __construct(string $brand)
{
$this->brand = $brand;
}

public function build(): MusicalInstrument
{
return (new AcousticGuitarFactory())
->createMusicalInstrument($this->brand);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;

use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\MusicalInstrumentStrategy;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Factory\ElectricGuitarFactory;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;

class ElectricGuitarStrategy implements MusicalInstrumentStrategy
{
private string $brand;

public function __construct(string $brand)
{
$this->brand = $brand;
}

public function build(): MusicalInstrument
{
return (new ElectricGuitarFactory())
->createMusicalInstrument($this->brand);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;

use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;

class MusicalInstrumentFactory
{
public function createMusicalInstrument(
MusicalInstrumentStrategy $musicalInstrument
): MusicalInstrument {
return $musicalInstrument->build();
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory;

use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\MusicalInstrument;

interface MusicalInstrumentStrategy
{
public function build(): MusicalInstrument;
}
6 changes: 4 additions & 2 deletions src/Creational/FactoryMethod/MusicalInstrumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ public function createMusicalInstrument(string $type, string $brand): MusicalIns
{
switch ($type) {
case self::ACOUSTIC_GUITAR:
return (new AcousticGuitarFactory())->createMusicalInstrument($brand);
return (new AcousticGuitarFactory())
->createMusicalInstrument($brand);
case self::ELECTRIC_GUITAR:
return (new ElectricGuitarFactory())->createMusicalInstrument($brand);
return (new ElectricGuitarFactory())
->createMusicalInstrument($brand);
default:
throw new \InvalidArgumentException('Invalid musical instrument type');
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Behavioral/Strategy/Payment/PaymentProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Tests\Behavioral\Strategy\Payment;

use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\CashPaymentMethod;
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\CreditCardPaymentMethod;
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\DebitCardPaymentMethod;
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\PaymentProcessor;
use PHPUnit\Framework\TestCase;

final class PaymentProcessorTest extends TestCase
{
public function testCanProcessPaymentWithCashPaymentMethod(): void
{
$paymentProcessor = new PaymentProcessor(new CashPaymentMethod());
$paymentProcessor->processPayment(100.00);

$this->assertEquals(90.00, $paymentProcessor->getAmount());
}

public function testCanProcessPaymentWithDebitCardPaymentMethod(): void
{
$paymentProcessor = new PaymentProcessor(new DebitCardPaymentMethod());
$paymentProcessor->processPayment(100.00);

$this->assertEquals(95.00, $paymentProcessor->getAmount());
}

public function testCanProcessPaymentWithCreditCardPaymentMethod(): void
{
$paymentProcessor = new PaymentProcessor(new CreditCardPaymentMethod());
$paymentProcessor->processPayment(100.00);

$this->assertEquals(100.00, $paymentProcessor->getAmount());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Tests\Behavioral\Strategy\Payment;

use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\CashPaymentMethod;
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\CreditCardPaymentMethod;
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\DebitCardPaymentMethod;
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\PaymentProcessorWithoutStrategy;
use PHPUnit\Framework\TestCase;

final class PaymentProcessorTest extends TestCase
{
public function testCanProcessPaymentWithCashPaymentMethod(): void
{
$paymentProcessor = new PaymentProcessorWithoutStrategy(
PaymentProcessorWithoutStrategy::PAYMENT_TYPE_CASH
);
$paymentProcessor->processPayment(100.00);

$this->assertEquals(90.00, $paymentProcessor->getAmount());
}

public function testCanProcessPaymentWithDebitCardPaymentMethod(): void
{
$paymentProcessor = new PaymentProcessorWithoutStrategy(
PaymentProcessorWithoutStrategy::PAYMENT_TYPE_DEBIT_CARD
);
$paymentProcessor->processPayment(100.00);

$this->assertEquals(95.00, $paymentProcessor->getAmount());
}

public function testCanProcessPaymentWithCreditCardPaymentMethod(): void
{
$paymentProcessor = new PaymentProcessorWithoutStrategy(
PaymentProcessorWithoutStrategy::PAYMENT_TYPE_CREDIT_CARD
);
$paymentProcessor->processPayment(100.00);

$this->assertEquals(100.00, $paymentProcessor->getAmount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Tests\Behavioral\Strategy\WithMethodFactory;

use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\AcousticGuitarStrategy;
use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\ElectricGuitarStrategy;
use Growthdev\DesignPatterns\Behavioral\Strategy\WithMethodFactory\MusicalInstrumentFactory;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\AcousticGuitar;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\ElectricGuitar;
use PHPUnit\Framework\TestCase;

final class MusicalInstrumentStrategyWithMethodFactoryTest extends TestCase
{
public function testCanCreateAcousticGuitar(): void
{
$factory = new MusicalInstrumentFactory();
$acousticGuitar = $factory->createMusicalInstrument(
new AcousticGuitarStrategy('Giannini Model')
);

$acousticGuitar->make();

$this->assertInstanceOf(AcousticGuitar::class, $acousticGuitar);
}

public function testCanCreateEletricGuitar(): void
{
$factory = new MusicalInstrumentFactory();
$eletricGuitar = $factory->createMusicalInstrument(
new ElectricGuitarStrategy('Tagima Model')
);

$eletricGuitar->make();

$this->assertInstanceOf(ElectricGuitar::class, $eletricGuitar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace DesignPatterns\Creational\FactoryMethod;
namespace Growthdev\DesignPatterns\Tests\Creational\FactoryMethod;

use Growthdev\DesignPatterns\Creational\FactoryMethod\MusicalInstrumentFactory;
use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\AcousticGuitar;
Expand Down