zoukankan      html  css  js  c++  java
  • AdapterPattern(适配器模式)

    import org.omg.PortableServer.AdapterActivator;
    
    /**
     * 分两种情况:
     * 1.类适配器
     * 2.对象适配器
     * 作用:让原本接口不兼容的两个类可以在一起工作
     * 比如说三孔插座适配两孔的,适配器只能配的更少,比如三孔配成两孔
     * @author TMAC-J
     *
     */
    1.类适配器 public class AdapterPattern { public class Adaptee{ public void doSomething(){ System.out.println("doSomething..."); } } interface Target{ void doOtherSomething(); } public class Adapter extends Adaptee implements Target{ @Override public void doOtherSomething() { super.doSomething(); } } public void test(){ Target target = new Adapter(); target.doOtherSomething(); } }

    2.对象适配器

          

    public class Adaptee{
    public void doSomething(){
    System.out.println("doSomething...");
    }
    }

    interface Target{
    void doOtherSomething();
    }

    public class Adapter extends Adaptee implements Target{

    private Adaptee adaptee;


    public Adapter(Adaptee adaptee) {
    this.adaptee = adaptee;
    }

    @Override
    public void doOtherSomething() {
    adaptee.doSomething();
    }
    }

    public void test(){
    Target target = new Adapter(new Adaptee());
    target.doOtherSomething();
    }



      

  • 相关阅读:
    openmediavault 5.5.23 安装插件失败的解决方案
    qt下载地址
    qt 5.12 增加 mysql驱动
    选基金标准
    关注几个基金
    调仓的几个问题
    要读的书
    ubuntu 20.04 LTS 安装webmin
    set的常见用法
    斜率优化dp([HNOI2008]玩具装箱)
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/6227215.html
Copyright © 2011-2022 走看看