------------------------对象适配器模式(组合模式)---------------------------------
public class AdapterTest {
public static void main(String[] args) {
Target target = new Adapter(new Adaptee());
target.output5v();
}
}
class Adaptee {
public int output220v() {
return 220;
}
}
interface Target {
int output5v();
}
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public int output5v() {
System.out.println("原始电压"+ adaptee.output220v() + ", 转换后电压: " + 5);
return 5;
}
}
--------------------------------类适配器模式(继承)-----------------------------------
public class AdapterTest {
public static void main(String[] args) {
Target target = new Adapter();
target.output5v();
}
}
class Adaptee {
public int output220v() {
return 220;
}
}
interface Target {
int output5v();
}
class Adapter extends Adaptee implements Target {
@Override
public int output5v() {
System.out.println("原始电压"+ output220v() + ", 转换后电压: " + 5);
return 5;
}
}