Exercises AdapterPattern Solution 2
Exercises AdapterPattern Solution 2
01 <?php
02 class PayPal {
03
04 public function __construct() {
05 // Your Code here //
06 }
07
08 public function sendPayment($amount) {
09 // Paying via Paypal //
10 echo "Paying via PayPal: ". $amount;
11 }
12 }
13
14 $paypal = new PayPal();
15 $paypal->sendPayment('2629');
In the above code, you can see that we are utilizing a PayPal class to simply pay the
amount. Here, we are directly creating the object of the PayPal class and paying via
PayPal. You have this code scattered in multiple places. So we can see that the code is
using the $paypal->sendPayment('<amount here>'); method to pay.
Some time ago, PayPal changed the API method name from sendPayment to payAmount. This
should clearly indicate a problem for those of us who have been using
the sendPayment method. Specifically, we need to change all sendPayment method
calls to payAmount. Imagine the amount of code we need to change and the time we need to
spend on testing each of the features once again.
class PayPal {
public PayPal() {
// Your Code here //
}
<?php
// Usage
$paypal = new PayPal();
$paypalAdapter = new PayPalAdapter($paypal);
$paypalAdapter->pay('2629');
<?php
// MoneyBooker class
class MoneyBooker {
public function __construct() {
// Your Code here //
}
// Usage
$paypal = new PayPal();
$moneyBooker = new MoneyBooker();
$paypalAdapter->pay('2629');
$moneyBookerAdapter->pay('1500');
// Usage
public class Main {
public static void main(String[] args) {
PayPal paypal = new PayPal();
PayPalAdapter paypalAdapter = new PayPalAdapter(paypal);
paypalAdapter.pay("2629");
}
}
3.
// Define the PaymentGateway interface
interface PaymentGateway {
void pay(String amount);
}
// Usage
public class Main {
public static void main(String[] args) {
PaymentEngine.pay("2629");
}
}