Last active
February 23, 2016 13:30
-
-
Save tarsislima/6bfe85c5530477599ea1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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