1. 介绍
适配器模式将某个类的接口转换成客户端期望的另一个接口,用户调用适配器转换出来的目标接口方法,适配器再调用被适配者的相关接口方法;
2. 实现方式
(1)类适配器模式
/** * @Description :被适配类 * @date :2024/4/6 15:56 */ public class Voltage220V { public Integer output220v(){ Integer src = 220; System.out.println("输出220v"); return src; } }
/** * @Description :适配接口 * @date :2024/4/6 15:58 */ public interface IVoltage5V { public Integer output5v(); }
/** * @Description :适配器类 * @date :2024/4/6 15:58 */ public class VoltageAdapter extends Voltage220V implements IVoltage5V { @Override public Integer output5v() { Integer src = output220v(); System.out.println("正常电压" + src / 44 + "v"); return src / 44; } }
public class Phone { public void charge(IVoltage5V iVoltage5V){ if (iVoltage5V.output5v() == 5){ System.out.println("电压可以充电!"); }else if (iVoltage5V.output5v() > 5){ System.out.println("电压大于5v"); } } }
/** * @Description :类适配器模式 * @date :2024/4/6 16:09 */ public class Client { public static void main(String[] args) { Phone phone = new Phone(); phone.charge(new VoltageAdapter()); } }
在示例中通过类适配器模式将输出220v的电压转换成输出5v的电压;
使用类适配器模式需要继承src类,要求dst类必须是接口,所以有一定的局限性
(2)对象适配器(推荐)
/** * @Description :被适配类 * @date :2024/4/6 15:56 */ public class Voltage220V { public Integer output220v(){ Integer src = 220; System.out.println("输出220v"); return src; } }
/** * @Description :适配接口 * @date :2024/4/6 15:58 */ public interface IVoltage5V { public Integer output5v(); }
/** * @Description :适配器类 * @date :2024/4/6 15:58 */ public class VoltageAdapter implements IVoltage5V { private Voltage220V voltage220V; public VoltageAdapter(Voltage220V voltage220V) { this.voltage220V = voltage220V; } @Override public Integer output5v() { int dest = 0; if (voltage220V != null){ Integer src = voltage220V.output220v(); dest = src / 44; } return dest; } }
/** * @Description : * @date :2024/4/6 16:03 */ public class Phone { public void charge(IVoltage5V iVoltage5V){ if (iVoltage5V.output5v() == 5){ System.out.println("电压可以充电!"); }else if (iVoltage5V.output5v() > 5){ System.out.println("电压大于5v"); } } }
/** * @Description :对象适配器模式 * @date :2024/4/6 16:09 */ public class Client { public static void main(String[] args) { Phone phone = new Phone(); phone.charge(new VoltageAdapter(new Voltage220V())); } }
对象适配器使用组合替代继承,解决了类适配器继承src局限性的问题,也不要求dst必须是接口,使用更灵活;
(3)接口适配器
public interface interface4 { public void m1(); public void m2(); public void m3(); public void m4(); }
public abstract class AbstractAdapter implements interface4{ @Override public void m1() { } @Override public void m2() { } @Override public void m3() { } @Override public void m4() { } }
/** * @Description :接口适配器模式 * @date :2024/4/6 16:46 */ public class Client { public static void main(String[] args) { AbstractAdapter adapter = new AbstractAdapter() { @Override public void m1() { System.out.println("接口适配器模式实现m1()"); } }; adapter.m1(); } }
接口适配器使用一个抽象类在不需要全部实现接口提供的方法时,为接口中的每一个方法提供一个默认方法,那么抽象类的子类就可以有选择性的覆盖父类中的方法
(4)springmvc中的适配器模式示例
//多种Controller实现 public interface Controller { } class HttpController implements Controller { public void doHttpHandler() { System.out.println("http..."); } } class SimpleController implements Controller { public void doSimplerHandler() { System.out.println("simple..."); } } class AnnotationController implements Controller { public void doAnnotationHandler() { System.out.println("annotation..."); } }
///定义一个Adapter接口 public interface HandlerAdapter { public boolean supports(Object handler); public void handle(Object handler); } // 多种适配器类 class SimpleHandlerAdapter implements HandlerAdapter { public void handle(Object handler) { ((SimpleController) handler).doSimplerHandler(); } public boolean supports(Object handler) { return (handler instanceof SimpleController); } } class HttpHandlerAdapter implements HandlerAdapter { public void handle(Object handler) { ((HttpController) handler).doHttpHandler(); } public boolean supports(Object handler) { return (handler instanceof HttpController); } } class AnnotationHandlerAdapter implements HandlerAdapter { public void handle(Object handler) { ((AnnotationController) handler).doAnnotationHandler(); } public boolean supports(Object handler) { return (handler instanceof AnnotationController); } }
import java.util.ArrayList; import java.util.List; public class DispatchServlet { public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>(); public DispatchServlet() { handlerAdapters.add(new AnnotationHandlerAdapter()); handlerAdapters.add(new HttpHandlerAdapter()); handlerAdapters.add(new SimpleHandlerAdapter()); } public void doDispatch() { // 此处模拟SpringMVC从request取handler的对象, // 适配器可以获取到希望的Controller // HttpController controller = new HttpController(); AnnotationController controller = new AnnotationController(); //SimpleController controller = new SimpleController(); // 得到对应适配器 HandlerAdapter adapter = getHandler(controller); // 通过适配器执行对应的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().doDispatch(); } }
标签:适配器,模式,public,class,handler,new,void From: https://www.cnblogs.com/homle/p/18117678