厚积薄发打卡Day33 :[kuangStudy] GoF23通俗易懂的设计模式之 <适配器模式>
发布日期:2021-04-30 21:05:28 浏览次数:82 分类:精选文章

本文共 2859 字,大约阅读时间需要 9 分钟。

???????

??

????????????????????????1995??GoF?Gang of Four?????????:???????????????23?????????????????????????????????

?????

?????????????????????????????????????????????????

????

??????????????

  • ??????Target????????????
  • ?????Adapter???????????????
  • ?????Adaptee?????????????????

???????USB??????????????USB?????????????

????

???????????????

  • ??????????????
  • ???????????????
  • ????

    ???????????????????

    ????

    // ?????????
    public class Cable {
    public void surfing() {
    System.out.println("???????>????~");
    }
    }
    // ??????????
    public interface CableToUSB {
    public void handleRequest();
    }
    // ????????
    public class Adapter extends Cable implements CableToUSB {
    @Override
    public void handleRequest() {
    super.surfing(); // ???????
    }
    }
    // ?????????????
    public class Computer {
    public void net(CableToUSB cableToUSB) {
    cableToUSB.handleRequest();
    }
    public static void main(String[] args) {
    Computer computer = new Computer();
    Adapter adapter = new Adapter();
    Cable cable = new Cable(); // ???????????
    computer.net(adapter); // ????
    // ???????>????~
    }
    }

    ?????

    // ?????????
    public class Cable {
    public void surfing() {
    System.out.println("???????>????~");
    }
    }
    // ??????????
    public interface CableToUSB {
    public void handleRequest();
    }
    // ????????
    public class Adapter implements CableToUSB {
    private Cable cable;
    public Adapter(Cable cable) {
    this.cable = cable;
    }
    @Override
    public void handleRequest() {
    cable.surfing(); // ???????
    }
    }
    // ?????????????
    public class Computer {
    public void net(CableToUSB cableToUSB) {
    cableToUSB.handleRequest();
    }
    public static void main(String[] args) {
    Computer computer = new Computer();
    Cable cable = new Cable(); // ??????
    Adapter adapter = new Adapter(cable); // ??????????
    computer.net(adapter); // ????
    // ???????>????~
    }
    }

    ???

    • ???

      • ?????????????
      • ???????????????????
    • ???

      • ???????????????????
      • ???????????????

    ????

    ?????????????

    • ?????????????????
    • ?????????????????????????

    ????

    Java??IO?

    Java??InputStreamReader?OutputStreamWriter?????????????

    • InputStreamReader???????????
    • OutputStreamWriter???????????

    SpringMVC??DispatcherServlet

    SpringMVC??HandlerAdapter?????????????????

    public interface HandlerAdapter {
    void handle(Request request, Response response, String handler);
    }

    Java????Enumeration

    Collections.enumeration()???????????????????

    public static 
    Enumeration
    enumeration(Collection
    c) {
    return new Enumeration
    () {
    Iterator
    i = c.iterator();
    public boolean hasMoreElements() { return i.hasNext(); }
    public T nextElement() { return i.next(); }
    };
    }

    ??????????????????????????????????????

    上一篇:Date类
    下一篇:Java多线程03:多线程深入+多线程综合实例

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2026年05月27日 22时09分08秒