- Spring 5 Design Patterns
- Dinesh Rajput
- 379字
- 2021-07-08 09:59:36
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.
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- Python量化投資指南:基礎、數據與實戰
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- 算法精粹:經典計算機科學問題的Java實現
- MySQL數據庫管理與開發實踐教程 (清華電腦學堂)
- HTML5入門經典
- PLC應用技術(三菱FX2N系列)
- Mastering Xamarin.Forms(Second Edition)
- MySQL程序員面試筆試寶典
- 從零開始學Selenium自動化測試:基于Python:視頻教學版
- UI動效設計從入門到精通
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Building Apple Watch Projects
- Python繪圖指南:分形與數據可視化(全彩)
- Mastering Magento Theme Design