设计模式 - 适配器模式
适配器模式:
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。
实际运用场景示例:
一个Sim卡管理系统需要对接三大运营商(移动,联通,电信),系统中有涉及查询SIm卡流量使用详情,Sim卡详情,Sim卡套餐查询等功能。三大运营商中均有提供对应的接口提供给Sim卡管理系统实现对应功能,如果不使用适配器模式的情况下,一般的做法是根据卡属于哪个运营商,则调用该运营商对应的接口。然而这种做法是存在弊端的,会让你的代码中存在很多繁杂且没必要的判断,比如你想开发sim卡流量使用详情展示功能点,这时候你的做法是判断这张卡是属于联通的则调用联通Sim卡流量使用详情接口,如果是电信的则使用电信的Sim卡流量使用详情接口,如果是移动的则调用移动的Sim卡流量使用详情接口。一个功能点,尚且可以接受,但是在系统中,所需要用的接口肯定不止一个,每次调用接口都需要写一段的代码判断调用哪个运营商的接口,这就很繁琐了。
解决办法:
引入适配器模式,业务模块只需要专注写业务代码,不需要去判断需要调用哪个运营商的接口,判断运营商的工作交由适配器统一完成
示例图:
示例代码:
统一实现接口:
public interface StrategyService {
String gainSimDetail(String ServiceName,String Message);
String gainSimUsedFlow(String ServiceName,String Message);
String gainSimFlowPoolList(String ServiceName,String Message);
}
联通:
public class UnicomServiceImpl implements StrategyService {
@Override
public String gainSimDetail(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is UnicomServiceImpl gainSimDetail ";
}
@Override
public String gainSimUsedFlow(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is UnicomServiceImpl gainSimUsedFlow ";
}
@Override
public String gainSimFlowPoolList(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is UnicomServiceImpl gainSimFlowPoolList ";
}
}
电信:
public class TelecomServiceImpl implements StrategyService {
@Override
public String gainSimDetail(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is TelecomServiceImpl gainSimDetail ";
}
@Override
public String gainSimUsedFlow(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is TelecomServiceImpl gainSimUsedFlow ";
}
@Override
public String gainSimFlowPoolList(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is TelecomServiceImpl gainSimFlowPoolList ";
}
}
移动:
public class MobileServiceImpl implements StrategyService {
@Override
public String gainSimDetail(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is MobileServiceImpl gainSimDetail ";
}
@Override
public String gainSimUsedFlow(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is MobileServiceImpl gainSimUsedFlow ";
}
@Override
public String gainSimFlowPoolList(String ServiceName,String Message) {
// TODO Auto-generated method stub
return "this is MobileServiceImpl gainSimFlowPoolList ";
}
}
适配器:
public class OperationAdapter implements StrategyService {
private StrategyService getServiceName(String ServiceName) throws Exception {
StrategyService strategyService;
if(ServiceName ==null) {
throw new Exception("ServiceName不能为空");
}
if("UnicomServiceImpl".equals(ServiceName)) {
strategyService=new UnicomServiceImpl();
}else if("MobileServiceImpl".equals(ServiceName)) {
strategyService= new MobileServiceImpl();
}else if("TelecomServiceImpl".equals(ServiceName)) {
strategyService= new TelecomServiceImpl();
}else {
strategyService=null;
}
return strategyService;
}
@Override
public String gainSimDetail(String ServiceName,String Message) {
// TODO Auto-generated method stub
try {
return getServiceName( ServiceName).gainSimDetail(ServiceName, Message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public String gainSimUsedFlow(String ServiceName,String Message) {
// TODO Auto-generated method stub
try {
return getServiceName( ServiceName).gainSimUsedFlow(ServiceName, Message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public String gainSimFlowPoolList(String ServiceName,String Message) {
// TODO Auto-generated method stub
try {
return getServiceName( ServiceName).gainSimFlowPoolList(ServiceName, Message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
测试从适配器中引用接口代码:
public class AdapterGainTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
OperationAdapter operationAdapter = new OperationAdapter();
String gainSimDetail = operationAdapter.gainSimDetail("UnicomServiceImpl", "test");
System.out.println(gainSimDetail);
String gainSimDetail2 = operationAdapter.gainSimDetail("MobileServiceImpl", "test");
System.out.println(gainSimDetail2);
String gainSimDetail3 = operationAdapter.gainSimDetail("TelecomServiceImpl", "test");
System.out.println(gainSimDetail3);
}
}