- 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.
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- 極簡算法史:從數學到機器的故事
- C#程序設計實訓指導書
- Delphi程序設計基礎:教程、實驗、習題
- AWS Serverless架構:使用AWS從傳統部署方式向Serverless架構遷移
- Java加密與解密的藝術
- 你不知道的JavaScript(中卷)
- MATLAB for Machine Learning
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Android玩家必備
- Lighttpd源碼分析
- ElasticSearch Cookbook(Second Edition)
- C專家編程
- Creating Data Stories with Tableau Public
- .NET 4.5 Parallel Extensions Cookbook