Last active
May 19, 2021 06:08
-
-
Save ksmylmz/395cb39e1acce905f3f914ec70bffb14 to your computer and use it in GitHub Desktop.
Strategy Design Pattern
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 | |
////1- Öncelikle ödeme için bir base tanımlayalım | |
interface IPayment | |
{ | |
public function pay(); | |
} | |
//2- Farklı Ödeme tiplerine göre implenetasyonları uygulayalım | |
class PayWithCard implements IPayment | |
{ | |
public function pay() | |
{ | |
echo "Paid with Debit Card"; | |
} | |
} | |
class PayCashOnDelivery implements IPayment | |
{ | |
public function pay() | |
{ | |
echo "Paid with Debit Card"; | |
} | |
} | |
class PayWithMailOrder implements IPayment | |
{ | |
public function pay() | |
{ | |
echo "Paid with Debit Card"; | |
} | |
} | |
//3- tanımlanacak Ödeme türüne göre stratejimizi uygulayacağımız | |
//Context sınıfını tanımlayalım | |
class PaymentContext | |
{ | |
private IPayment $collector; | |
public function setCollector(IPayment $collector) | |
{ | |
$this->collector = $collector; | |
} | |
public function getPayment() | |
{ | |
$collector->pay(); | |
} | |
} | |
///4- farklı ödeme tipleri içi enumaration sınıfımızı yaratalım | |
abstract class PaymetType | |
{ | |
const CARD=new PayWithCard(); | |
const CASHONDELIVERY=new PayWithCard(); | |
const MAILORDER=new PayWithCard(); | |
} | |
///5- Manager sınıfımızda bize iletilmiş ödeme tipine göre, | |
///Contextimizi set edelim ve ödememiizi alalım | |
class PaymentController | |
{ | |
public function getPayment(Request $request) | |
{ | |
$PaymentContext = new PaymentContext(); | |
$PaymentContext->setCollector($request->paymentType); | |
$PaymentContext->getPayment(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment