






Preview text:
lOMoAR cPSD| 59078336 The Problem 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(''); 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. 1. Rewrite the code in Java
2. Use Adapter Pattern to change $paypal->sendPayment('2629'); into
$paypal>pay('2629'); and the code will never change even if Paypal changed
their API from payAmount to depositMoney, for instance.
3. Apart from Paypal, now we have a MoneyBooker class, which offers payMoney(int)
to perform online transactions. Extend your Adapter-based program to handle this situation. Solution:
1. Equivalent code in Java: lOMoAR cPSD| 59078336 class PayPal { public PayPal() { // Your Code here // }
public void sendPayment(String amount) { // Paying via PayPal //
System.out.println("Paying via PayPal: " + amount); } }
public class Main {
public static void main(String[] args) {
PayPal paypal = new PayPal(); paypal.sendPayment("2629"); } }
2. My solution is to define an Adapter class, inherited from an interface.
In this Adapter class, we will define method pay and call API sendPayment (or payAmount,
depositMoney). The code used from the client will never change, just change the code inside Adapter class. <?php
// Define the PayPal general interface (polymorphism purpose) interface PaymentGateway
{ public function pay($amount); }
// PayPal adapter implementing the PaymentGateway interface class PayPalAdapter
implements PaymentGateway { private $paypal;
public function __construct(PayPal $paypal) { $this->paypal = $paypal; }
public function pay($amount) {
// Adapt the method call to the new API $this->paypal->sendPayment($amount); } }
// Existing PayPal class class PayPal {
public function __construct() { lOMoAR cPSD| 59078336 // Your Code here // }
public function sendPayment($amount) { // Paying via Paypal //
echo "Paying via PayPal: ". $amount; } } // Usage $paypal = new PayPal();
$paypalAdapter = new PayPalAdapter($paypal);
$paypalAdapter->pay('2629');
3. Extending Adapter-based program: <?php
// Define the PayPal general interface (polymorphism purpose) interface PaymentGateway
{ public function pay($amount); }
// PayPal adapter implementing the PaymentGateway interface class PayPalAdapter
implements PaymentGateway { private $paypal;
public function __construct(PayPal $paypal) { $this->paypal = $paypal; }
public function pay($amount) {
// Adapt the method call to the PayPal API $this->paypal->sendPayment($amount); } }
// MoneyBooker adapter implementing the PaymentGateway interface class
MoneyBookerAdapter implements PaymentGateway { private $moneyBooker;
public function __construct(MoneyBooker $moneyBooker) {
$this->moneyBooker = $moneyBooker; }
public function pay($amount) {
// Adapt the method call to the MoneyBooker API
$this->moneyBooker->payMoney($amount); } } // Existing PayPal class lOMoAR cPSD| 59078336 class PayPal {
public function __construct() { // Your Code here // }
public function sendPayment($amount) { // Paying via Paypal //
echo "Paying via PayPal: ". $amount; } } // MoneyBooker class class MoneyBooker {
public function __construct() { // Your Code here // }
public function payMoney($amount) { // Paying via MoneyBooker //
echo "Paying via MoneyBooker: ". $amount; } } // Usage $paypal = new PayPal();
$moneyBooker = new MoneyBooker();
$paypalAdapter = new PayPalAdapter($paypal);
$moneyBookerAdapter = new MoneyBookerAdapter($moneyBooker);
$paypalAdapter->pay('2629');
$moneyBookerAdapter->pay('1500');
●Java code for reference (if needed): 2. // Define the PaymentGateway
interface interface PaymentGateway {
void pay(String amount); }
// PayPal adapter implementing the PaymentGateway interface
class PayPalAdapter implements PaymentGateway { private PayPal paypal;
public PayPalAdapter(PayPal paypal) { this.paypal = paypal; }
public void pay(String amount) {
// Adapt the method call to the new API paypal.sendPayment(amount); lOMoAR cPSD| 59078336 } } // Existing PayPal class class PayPal { public PayPal() { // Your Code here // }
public void sendPayment(String amount) { // Paying via PayPal //
System.out.println("Paying via PayPal: " + amount); } } // Usage
public class Main {
public static void main(String[] args) {
PayPal paypal = new PayPal();
PayPalAdapter paypalAdapter = new PayPalAdapter(paypal); paypalAdapter.pay("2629"); } } 3. lOMoAR cPSD| 59078336
// Define the PaymentGateway interface
interface PaymentGateway {
void pay(String amount); }
// PayPal adapter implementing the PaymentGateway
interface class PayPalAdapter implements PaymentGateway { private PayPal paypal;
public PayPalAdapter(PayPal paypal)
{ this.paypal = paypal; }
public void pay(String amount) {
// Adapt the method call to the PayPal API paypal.sendPayment(amount); } }
// MoneyBooker adapter implementing the PaymentGateway
interface class MoneyBookerAdapter implements PaymentGateway {
private MoneyBooker moneyBooker;
public MoneyBookerAdapter(MoneyBooker moneyBooker)
{ this.moneyBooker = moneyBooker; }
public void pay(String amount) {
// Adapt the method call to the MoneyBooker API
moneyBooker.payMoney(amount); lOMoAR cPSD| 59078336 } } // Existing PayPal class class PayPal { public PayPal() { // Your Code here // }
public void sendPayment(String amount) { // Paying via Paypal //
System.out.println("Paying via PayPal: " + amount); } } // Existing MoneyBooker class class MoneyBooker { public MoneyBooker() { // Your Code here // }
public void payMoney(String amount) { // Paying via MoneyBooker //
System.out.println("Paying via MoneyBooker: " + amount); } } // Usage
public class Main {
public static void main(String[] args) {
PaymentGateway PaymentEngine = selectPaymentMethod(); // either Paypal or Monkey PaymentEngine.pay("2629"); } }