類(lèi)圖
適配器模式.png
實(shí)現(xiàn)
客戶(hù)端調(diào)用
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();
}
}
輸出
原來(lái)的方法
適配器類(lèi)中的測(cè)試方法
被適配的方法
Process finished with exit code 0
原來(lái)的接口OriginClassInterface
package com.company;
public interface OriginClassInterface {
void testMethod();
}
適配的類(lèi)Adapter
package com.company;
public class Adapter extends AdapteredClass implements OriginClassInterface {
@Override
public void testMethod() {
System.out.println("適配器類(lèi)中的測(cè)試方法");
adapteredTestMethod();
}
}
被適配的類(lèi)AdapteredClass
package com.company;
public class AdapteredClass {
public void adapteredTestMethod() {
System.out.println("被適配的方法");
}
}
非適配類(lèi)NonAdapter
package com.company;
public class NonAdapter implements OriginClassInterface {
@Override
public void testMethod() {
System.out.println("原來(lái)的方法");
}
}