Skip to content

Instantly share code, notes, and snippets.

@tarsislima
Last active February 23, 2016 13:30
Show Gist options
  • Save tarsislima/6bfe85c5530477599ea1 to your computer and use it in GitHub Desktop.
Save tarsislima/6bfe85c5530477599ea1 to your computer and use it in GitHub Desktop.
<?php
abstract class PaymentType {
abstract function pay();
}
class BitcoinPayment extends PaymentType {
public function pay()
{
return 'Pay with Bitcoin';
}
}
class MoneyPayment extends PaymentType {
public function pay()
{
return 'pay with Money';
}
}
class Factory {
public static function create($name)
{
$className = $name.'Payment';
if (class_exists($className))
{
return new $className();
}
}
}
// injeção
class PaymentsService {
//----- Recebe um tipo Payment independente de qual for
public function payWith(PaymentType $payment)
{
return $payment->pay();
}
}
class Controller {
public function action()
{
$service = new PaymentService();
echo $service->payWith(Factory::create('Money'));
echo $service->payWith(Factory::create('Bitcoin'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment