适配器模式

 

 

 

 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();
}
}
posted @ 2020-04-09 16:19  Curedfisher  阅读(126)  评论(0编辑  收藏  举报