• 适配器模式


     

     TypeC

    package com.qianfeng.adapter;

    public interface TypeC {
    String getInfo();
    }
    -----------------------------------------
    USB
    package com.qianfeng.adapter;

    public interface USB {
    String getInfo();
    }
    -------------------------------------------------
    Computer.java
    package com.qianfeng.adapter;

    public class Computer {
    private USB usb1;

    public void connect(USB usbDevice){
    this.usb1=usbDevice;
    }

    public void showInfo(){
    System.out.println(this.usb1.getInfo());
    }
    }
    --------------------------------------------------------------------
    Phone.java
    package com.qianfeng.adapter;

    public class Phone implements TypeC{

    @Override
    public String getInfo() {
    return "这是一个手机";
    }
    }
    ---------------------------------------------------------------------
    AdapterJobMain.java
    package com.qianfeng.adapter;

    public class AdapterJobMain {
    //需求:
    //电脑usb接口,Type-C接口的手机连接到电脑上
    public static void main(String[] args) {
    Computer computer=new Computer();
    Phone phone=new Phone();
    //电脑通过适配器连接到了一个TypeC手机
    computer.connect(new Adapter(phone));
    computer.showInfo();
    }

    }

     --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Adapter.java

    package com.qianfeng.adapter;

    /**
    * 中间件:适配器
    */
    public class Adapter implements USB{

    private TypeC device;

    public Adapter(TypeC device){
    this.device=device;
    }
    @Override
    public String getInfo() {
    return this.device.getInfo();
    }
    }
  • 相关阅读:
    浏览器 显示flash问题
    类型参数的约束
    C# FUNC 应用
    c#抽奖系统
    3D基础数学小结
    google应用之字体引用
    MYSQL启动参数
    chrome中你不知道的快捷方式
    SQL Server 2008在添加用户时弹出15195错误
    Hibernate Maven Missing artifact javax.transaction:jta:jar:1.0.1B
  • 原文地址:https://www.cnblogs.com/curedfisher/p/12667691.html
Copyright © 2020-2023  润新知