一、前言:
主要分为三类:类适配器模式、对象的适配器模式、接口的适配器模式。
二、类适配器模式:
2.1、UML图:
2.2、代码实现:
Step 1) 创建 Phone 类:
public class Phone {
public void charging(Voltage5V voltage5V){
if(voltage5V.output5V() == 5){
System.out.println("电压为 5V 可以充电 !");
}else if(voltage5V.output5V() > 5) {
System.out.println("电压大于 5V 不能充电 !");
}
}
}
Step 2) 创建 Voltage5V 接口:
public interface Voltage5V {
public int output5V();
}
Step 3) 创建 Voltage220V 类:
public class Voltage220V {
public int output220V(){
int src = 220;
System.out.println("电压为:" + src + "伏");
return src;
}
}
Step 4) 创建 VoltageAdapter 类:
public class VoltageAdapter extends Voltage220V implements Voltage5V{
@Override
public int output5V() {
int srcV = output220V();
int dstV = srcV/44;
return dstV;
}
}
Step 5) 创建 main 方法:
public class Client {
public static void main(String[] args) {
System.out.println("== 类适配器 ===");
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}
三、对象适配器模式:
3.1、UML图:
3.2、代码实现:
Step 1) 创建 Phone 类:
public class Phone {
public void charging(Voltage5V voltage5V){
if(voltage5V.output5V() == 5){
System.out.println("电压为 5V 可以充电 !");
}else if(voltage5V.output5V() > 5) {
System.out.println("电压大于 5V 不能充电 !");
}
}
}
Step 2) 创建 Voltage5V 接口:
public interface Voltage5V {
public int output5V();
}
Step 3) 创建 Voltage220V 类:
public class Voltage220V {
public int output220V(){
int src = 220;
System.out.println("电压为:" + src + "伏");
return src;
}
}
Step 4) 创建 VoltageAdapter 类:
public class VoltageAdapter implements Voltage5V {
private Voltage220V voltage220V;
public VoltageAdapter(Voltage220V voltage220V){
this.voltage220V = voltage220V;
}
@Override
public int output5V() {
int dst = 0;
if(null != voltage220V){
int src = voltage220V.output220V();
System.out.println("使用对象适配器,进行适配!");
dst = src / 44;
System.out.println("适配器完成!");
}
return dst;
}
}
Step 5) 创建 main 方法:
public class Client {
public static void main(String[] args) {
System.out.println("== 对象适配器 ===");
Phone phone = new Phone();
phone.charging(new VoltageAdapter(new Voltage220V()));
}
}
四、接口适配器模式:
4.1、UML图:
4.2、代码实现:
Step 1) 创建 Controller 接口、及其 HttpController、SimleController、AnnotationController 实现类:
public interface Controller {
}
class HttpController implements Controller {
public void doHttpHandler() {
System.out.println("http...");
}
}
class SimleController implements Controller {
public void doSimplerHandler() {
System.out.println("simple...");
}
}
class AnnotationController implements Controller {
public void doAnnotationController() {
System.out.println("annotation");
}
}
Step 2) 创建 HandlerAdapter 接口、及其 SimpleHanderAdaper、HttpHandlerAdaper、AnnotationHandlerAdaper 实现类:
public interface HandlerAdapter {
public boolean supports(Object handler);
public void handle(Object hander);
}
class SimpleHanderAdaper implements HandlerAdapter{
@Override
public boolean supports(Object handler) {
return (handler instanceof SimleController);
}
@Override
public void handle(Object hander) {
((SimleController) hander).doSimplerHandler();
}
}
class HttpHandlerAdaper implements HandlerAdapter{
@Override
public boolean supports(Object handler) {
return (handler instanceof HttpController);
}
@Override
public void handle(Object hander) {
((HttpController) hander).doHttpHandler();
}
}
class AnnotationHandlerAdaper implements HandlerAdapter{
@Override
public boolean supports(Object handler) {
return (handler instanceof AnnotationController);
}
@Override
public void handle(Object hander) {
((AnnotationController) hander).doAnnotationController();;
}
}
Step 3) 创建 DispatchServlet 类、且与 main 方法:
public class DispatchServlet {
public static List<HandlerAdapter> handlerAdapters = new ArrayList<>();
public DispatchServlet(){
handlerAdapters.add(new AnnotationHandlerAdaper());
handlerAdapters.add(new HttpHandlerAdaper());
handlerAdapters.add(new SimpleHanderAdaper());
}
public void doDisptch(){
SimleController controller = new SimleController();
// HttpController controller = new HttpController();
// AnnotationController controller = new AnnotationController();
HandlerAdapter adapter = getHandler(controller);
adapter.handle(controller);
}
public HandlerAdapter getHandler(Controller controller){
for(HandlerAdapter adapter : this.handlerAdapters){
if(adapter.supports(controller)){
return adapter;
}
}
return null;
}
public static void main(String[] args) {
new DispatchServlet().doDisptch();
}
}
• 由 ChiKong_Tam 写于 2020 年 10 月 18 日