Sample implementation of the adapter design pattern

I am going to create an example that shows the actual demonstration of the adapter design pattern, so let's discuss this example, I am creating this example based on making payment through a payment gateway. Suppose I have one old payment gateway and also have the latest advanced payment gateway, and both gateways are unrelated to each other, so my requirement is, I want to migrate from the old payment gateway to an advanced payment gateway while changing my existing source code. I am creating an adapter class to solve this problem. This adapter class is working as a bridge between two different payment gateways, let's look at the following code:

Let's now create an interface for the old payment gateway:

    package com.packt.patterninspring.chapter3.adapter.pattern; 
    import com.packt.patterninspring.chapter3.model.Account; 
    public interface PaymentGateway { 
      void doPayment(Account account1, Account account2); 
    } 

Let's now create an implementation class for the old payment gateway PaymentGateway.java:

    package com.packt.patterninspring.chapter3.adapter.pattern; 
    import com.packt.patterninspring.chapter3.model.Account; 
    public class PaymentGatewayImpl implements PaymentGateway{ 
      @Override 
      public void doPayment(Account account1, Account account2){ 
         System.out.println("Do payment using Payment Gateway"); 
      } 
    } 

The following interface and its implementation have new and advanced functionalities for the payment gateway:

    package com.packt.patterninspring.chapter3.adapter.pattern; 
    public interface AdvancedPayGateway { 
      void makePayment(String mobile1, String mobile2); 
    } 

Let's now create an implementation class for the advance payment gateway interface:

    package com.packt.patterninspring.chapter3.adapter.pattern; 
    import com.packt.patterninspring.chapter3.model.Account; 
    public class AdvancedPaymentGatewayAdapter implements 
AdvancedPayGateway{ private PaymentGateway paymentGateway; public AdvancedPaymentGatewayAdapter(PaymentGateway
paymentGateway) { this.paymentGateway = paymentGateway; } public void makePayment(String mobile1, String mobile2) { Account account1 = null;//get account number by
mobile number mobile Account account2 = null;//get account number by
mobile number mobile paymentGateway.doPayment(account1, account2); } }

Let's see a demo class for this pattern as follows:

    package com.packt.patterninspring.chapter3.adapter.pattern; 
    public class AdapterPatternMain { 
      public static void main(String[] args) { 
        PaymentGateway paymentGateway = new PaymentGatewayImpl(); 
        AdvancedPayGateway advancedPayGateway = new 
AdvancedPaymentGatewayAdapter(paymentGateway); String mobile1 = null; String mobile2 = null; advancedPayGateway.makePayment(mobile1, mobile2); } }

In the preceding class, we have the old payment gateway object as the PaymentGateway interface, but we convert this old payment gateway implementation to the advanced form of the payment gateway by using the AdvancedPaymentGatewayAdapter adapter class. Let's run this demo class and see the output as follows:

Now that we've seen the adapter design pattern, let's turn to a different variant of it--the Bridge design pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset