类图
适配器模式.png实现
客户端调用
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
OriginClassInterface originalObject = new NonAdapter();
originalObject.testMethod();
OriginClassInterface adapteredObject = new Adapter();
adapteredObject.testMethod();
}
}
输出
原来的方法
适配器类中的测试方法
被适配的方法
Process finished with exit code 0
OriginClassInterface接口
package com.company;
public interface OriginClassInterface {
void testMethod();
}
被适配的类AdapteredClass
package com.company;
public class AdapteredClass {
public void adapteredTestMethod() {
System.out.println("被适配的方法");
}
}
非适配的类NonAdapter
package com.company;
public class NonAdapter implements OriginClassInterface {
@Override
public void testMethod() {
System.out.println("原来的方法");
}
}
适配了的类Adapter
package com.company;
public class Adapter implements OriginClassInterface {
private AdapteredClass adapteredClass = new AdapteredClass();
@Override
public void testMethod() {
System.out.println("适配器类中的测试方法");
this.adapteredClass.adapteredTestMethod();
}
}
网友评论