适配器:当复用一个存在的类A或者创建一个复用的类 ,它的
接口不符合你的需求,采用此模式可以不在用子类,而是面向接口的原则进行使用
存在的类
package com.adapterpattern;
public class A {
public void testA(){
System.out.println("THIS IS TESTA");
}
}
接口
package com.adapterpattern;
public interface Target {
public void testA();
public void testB();
}
实现类
package com.adapterpattern;
public class Aadapter implements Target {
private A a = new A();
public Aadapter(A a) {
this.a = a;
}
@Override
public void testA() {
this.a.testA();
}
@Override
public void testB() {
System.out.println("THIS IS TESTB");
}
}
测试类
package com.adapterpattern;
public class Test {
public static void main(String[] args) {
Target a = new Aadapter(new A());
a.testA();
a.testB();
}
}
输出
THIS IS TESTA
THIS IS TESTB