亲宝软件园·资讯

展开

Java适配器模式

爱学习的大鱼​​​​​​​ 人气:0

基本介绍

工作原理

类适配器模式

背景介绍:在中国电源大都是220伏的,而日常生活中手机的充电确是5伏1安(苹果),通过类适配器模拟电源适配器实现手机充电功能

Voltage220V(被适配电源)

public class Voltage220V {

    public int output220V() {
        int src = 220;
        System.out.println("电压 = " + src + "伏");
        return src;
    }
}

Voltage5V(目标输出)

public interface Voltage5V {
    int output5V();
}

VoltageAdapter(电源适配器)

public class VoltageAdapter extends Voltage220V implements Voltage5V {
    @Override
    public int output5V() {
        int src = output220V();
        int dst = src / 44;
        System.out.printf("适配器将%d伏====>%d伏\n", src, dst);
        return dst;
    }
}

Phone(手机充电)

public class Phone {
    public void charging(Voltage5V v) {
        int src = v.output5V();
        if (src <= 5 && src > 0) {
            System.out.println("电压 = " + src + "伏,可以充电...");
        } else if (src > 5) {
            System.out.println("电压过高");
        } else {
            System.out.println("没有充电...");
        }
    }
}

Client

public class Client {
    public static void main(String[] args) {
        System.out.println("====类适配器模式====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter());
    }
}

实现结果:

类适配器模式注意事项和细节

对象适配器模式

优点:

UML类图:

其他代码与上一致,修改电源适配器即可:

public class VoltageAdapter implements Voltage5V {
    private Voltage220V voltage220V = null;
    public VoltageAdapter(Voltage220V voltage220V) {
        this.voltage220V = voltage220V;
    }
    @Override
    public int output5V() {
        int src = 0;
        if (voltage220V != null) {
            src = voltage220V.output220V();
        }
        int dst = src / 44;
        System.out.printf("适配器将%d伏====>%d伏\n",src,dst);
        return dst;
    }
}

Client(测试)

public class Client {
    public static void main(String[] args) {
        System.out.println("====对象适配器模式====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter(new Voltage220V()));
        System.out.println("--------------------");
        phone.charging(new VoltageAdapter(null));
    }
}

实现结果:

接口适配器模式

简介:

UML类图

背景介绍:模拟springMVC源码展示接口设计模式

接口适配器和实现类

///定义一个Adapter接口 
public interface HandlerAdapter {
   public boolean support(Controller handler);
   public void handle(Controller handler);
}
// 多种适配器类
class SimpleHandlerAdapter implements HandlerAdapter {
   public void handle(Controller handler) {
       handler.doHandler();
   }
   public boolean support(Controller handler) {
      return (handler instanceof SimpleController);
   }
}
class HttpHandlerAdapter implements HandlerAdapter {
   public void handle(Controller handler) {
      handler.doHandler();
   }
   public boolean support(Controller handler) {
      return (handler instanceof HttpController);
   }

}
class AnnotationHandlerAdapter implements HandlerAdapter {
   public void handle(Controller handler) {
      handler.doHandler();
   }

   public boolean support(Controller handler) {

      return (handler instanceof AnnotationController);
   }
}

模拟Controller

//多种Controller实现  
public interface Controller {
   void doHandler();
}
class HttpController implements Controller {
   @Override
   public void doHandler() {
      System.out.println("http...");
   }
}
class SimpleController implements Controller {
   @Override
   public void doHandler() {
      System.out.println("simple...");
   }
}
class AnnotationController implements Controller {
   @Override
   public void doHandler() {
      System.out.println("annotation...");
   }
}

模拟DispatchServlet

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(Controller controller) {
      // 得到对应适配器
      HandlerAdapter adapter = getHandler(controller);
      // 通过适配器执行对应的controller对应方法
      if (adapter != null) {
         adapter.handle(controller);
      } else {
         System.out.println("没有该适配器...");
      }

   }
   public HandlerAdapter getHandler(Controller controller) {
      //遍历:根据得到的controller(handler), 返回对应适配器
      for (HandlerAdapter adapter : this.handlerAdapters) {
         if (adapter.support(controller)) {
            return adapter;
         }
      }
      return null;
   }

   public static void main(String[] args) {
      new DispatchServlet().doDispatch(new SimpleController() ); 
		new DispatchServlet().doDispatch(new HttpController() ); 
		new DispatchServlet().doDispatch(new AnnotationController()); 
   }

}

实现结果:

适配器模式的注意事项和细节

加载全部内容

相关教程
猜你喜欢
用户评论