WelcomeTo
WelcomeTo

Reputation: 20571

Using "Adapter" pattern

How I understand, the Goal of the Adapter pattern is to call some class methods using some interface (which opened to clients). To make adapter pattern we need to implement some interface (which uses by client), and also we need to extend some class, which methods client need to call when calling interface methods.

class Adapter extends NeedClass implements PublicInterface{}

But what if we haven't interface, but have only 2 classes? For example we have some class(not interface!) which methods uses clients. Now we need to call methods of other class by making adapter class, but we cant to do this, because we cant make multiple Inheritance on the adapter class.

class Adapter extends NeedClass, PublicInterface

above code doesnt work. What we can do in this case?

Upvotes: 2

Views: 1088

Answers (3)

mwangaben
mwangaben

Reputation: 957

From what i have understood the Adapter Pattern. it is helpful when dealing with the third part codes such as API which is/ are subject to changes any time and my likely to break your code if implemented direct. For example : Using Paypal in your site for payment online.let's assume the Paypal uses the method payMoney() for payment. and after sometime they decide to change the method to something else let's say sendMoney(). This is likely to break your code if implemented directly, with the use of Adapter Design pattern this can be solves as follow

the third part code => Paypal

   class Paypal {
     public function __construct(){
          // their codes 
     }
   public function payMoney($amount){
      // the logic of validating
      // the $amount variables and do the payment
   }   

}

so implement it directly in the code as below will break the code

$pay = new Paypal();
$pay->payMoney(200);

using adapter will save numbers of hours and a complex work of updating the code from payMoney() to sendMoney() in every where that the API scripts has been implemented. Adapter enable update in one place and that's it. Let see it.

  class paypalAdapter {

     private $paypal;
     // Paypal object into construct and check if it's pa
     // Paypal object via type hint
     public function __construct(PayPal $paypal) {
        $this->paypal = $paypal;
     }
     // call the Paypal method in your own 
     //custom method that is to be  
     // implemented directly into your code
     public function pay($amount) {
        $this->paypal->payMoney($amount);
}

}

so it is like that and there you can go and use the PaypalAdater directly into the code as follow;

  $pay = new PaypalAdapter(new Paypal);
  $pay->pay(200);

So in future when the Vendor(Paypal) decide to use sendMoney instead of payMoney what to be done is to open the PaypalAdapter class and do the following in the pay($amount) method:

     // SEE THIS METHOD ABOVE TO OBSERVE CHANGES
     // FROM $this->paypal->payMoney($amount);
     //  TO $this->paypal->senMoney($amount);
     public function pay($amount) {
        $this->paypal->sendMoney($amount);
}   

After this minor change in one place, everything works well as before.

Upvotes: 2

hakyer
hakyer

Reputation: 454

You can use a composition instead of inheritance. Add a field to Adapter class of type NeedClass:

public class Adapter extends PublicInterface {
    private NeedClass needClass;
}

Then inside Adapter methods delegate execution to needClass field.

Upvotes: 2

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

You can has an instance of NeedClass in Adapter and call it, when you need. So you extend only from PublicInterface.

public class Adapter extends PublicInterface {

    private NeedClass needClass;

    @Override
    public void doSomething() {
        needClass.doSomethingElse("someParameter");
    }
}

Upvotes: 3

Related Questions