广州顺德网站建设,网站建设运维标准,龙华做网站联系电话,建设主题网站的顺序是什么意思适配器模式将一个类的接口转换成客户端所期望的另一个接口#xff0c;解决由于接口不兼容而无法进行合作的问题。
设计基本步骤
1. 创建目标接口#xff08;Target Interface#xff09;#xff0c;该接口定义了客户端所期望的方法。
2.创建被适配类#xff08;Adaptee…适配器模式将一个类的接口转换成客户端所期望的另一个接口解决由于接口不兼容而无法进行合作的问题。
设计基本步骤
1. 创建目标接口Target Interface该接口定义了客户端所期望的方法。
2.创建被适配类Adaptee Class该类是需要被适配的类它包含了一些已经存在的方法。
3. 创建适配器类Adapter Class该类实现了目标接口并包含被适配对象的引用。
4. 在适配器类中实现目标接口的方法并在方法内部调用被适配类的方法。 实例介绍运用
假设我们正在开发一个电子支付业务该系统需要与不同的支付服务提供商进行集成支付宝、微信支付和银联支付每个支付服务提供商都有自己的接口和方法来处理支付请求我们希望将支付服务提供商的接口适配成了统一的支付接口转换我们可以使用适配器模式实现这样三种支付方式我们都能同时处理。
1. 创建目标接口
public interface PaymentService {void pay(String paymentType, double amount);
}
2.创建被适配类
public class AlipayService implements PaymentService {//支付宝支付Overridepublic void pay(String paymentType, double amount) {System.out.println(Alipay payment: amount CNY);}
}public class WechatPayService implements PaymentService {//微信支付Overridepublic void pay(String paymentType, double amount) {System.out.println(WeChat payment: amount CNY);}
}public class UnionPayService implements PaymentService {//银联支付Overridepublic void pay(String paymentType, double amount) {System.out.println(UnionPay payment: amount CNY);}
}
3. 创建适配器类、实现方法
public class PaymentAdapter implements PaymentService {//被适配对象引用private AlipayService alipayService;private WechatPayService wechatPayService;private UnionPayService unionPayService;//初始化public PaymentAdapter() {alipayService new AlipayService();wechatPayService new WechatPayService();unionPayService new UnionPayService();}Overridepublic void pay(String paymentType, double amount) {//实现统一支付逻辑if (paymentType.equalsIgnoreCase(Alipay)) {alipayService.pay(paymentType, amount);} else if (paymentType.equalsIgnoreCase(WeChatPay)) {wechatPayService.pay(paymentType, amount);} else if (paymentType.equalsIgnoreCase(UnionPay)) {unionPayService.pay(paymentType, amount);} else {//其他方式不支持System.out.println(Unsupported payment type: paymentType);}}
}
4.客户端简单实现
public class Main {public static void main(String[] args) {PaymentService paymentService new PaymentAdapter();paymentService.pay(Alipay, 10000.0);paymentService.pay(WeChatPay, 20000.0);paymentService.pay(UnionPay, 30000.0);paymentService.pay(ApplePay, 500.0);}
} 文章转载自: http://www.morning.nqbpz.cn.gov.cn.nqbpz.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.sbwr.cn.gov.cn.sbwr.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.rwlsr.cn.gov.cn.rwlsr.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.gidmag.com.gov.cn.gidmag.com