1、Client.java
public class Client { public static void main(String[] args) { System.out.println("====对象适配器模式===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Voltage220V())); // 通过这里来进行对数据的传输。 } }2、Phone.java
public class Phone { public void charging(Voltage5V voltage5V){ if (voltage5V.output() == 5){ System.out.println("正在充电~~"); }else if(voltage5V.output() > 5){ System.out.println("无法充电"); } } }3、Voltage5V.java
public interface Voltage5V { // 输出5V public abstract int output(); }4、Voltage220V.java
public class Voltage220V { // 输出220V电压,不变 public int output220V(){ int src = 220; System.out.println("电压 = " + src + "伏"); return src; } }5、VoltageAdapter.java
public class VoltageAdapter implements Voltage5V{ private Voltage220V voltage220V; public VoltageAdapter(Voltage220V voltage220V) { this.voltage220V = voltage220V; } @Override public int output() { int des = 0; if(voltage220V != null){ int src = voltage220V.output220V(); // 获取220V电压 System.out.println("使用适配器,进行适配~~"); des = src / 44; System.out.println("适配完成,输出的电压为=" + des + "伏"); } return des; } }