適配器模式(一)
單項適配器模式:package com.jerry.design.adapter1.imp;
public interface InterfaceA {
public void testA();
}
接口實現(xiàn)類:
package com.jerry.design.adapter1.impl;
import com.jerry.design.adapter1.imp.InterfaceA;
public class ImplA implements InterfaceA{
@Override
public void testA() {
System.out.println(" i am do something as InterfaceA!");
}
}
目標(biāo)接口:
package com.jerry.design.adapter1.imp;
public interface InterfaceB {
public void testB();
}
目標(biāo)接口實現(xiàn)類:
package com.jerry.design.adapter1.impl;
import com.jerry.design.adapter1.imp.InterfaceA;
import com.jerry.design.adapter1.imp.InterfaceB;
public class ImplB implements InterfaceB{
private InterfaceA implA;
public ImplB(InterfaceA implA){
this.implA = implA;
}
@Override
public void testB() {
implA.testA();
}
}
測試方法:
package com.jerry.design.adapter1.client;
import com.jerry.design.adapter1.imp.InterfaceA;
import com.jerry.design.adapter1.imp.InterfaceB;
import com.jerry.design.adapter1.impl.ImplA;
import com.jerry.design.adapter1.impl.ImplB;
public class Test {
public static void main(String[] args) {
InterfaceA implA = (InterfaceA) new ImplA();
InterfaceB implB = (InterfaceB) new ImplB(implA);
implB.testB();// i am do something as InterfaceA!
}
}
總結(jié):原接口轉(zhuǎn)換為目標(biāo)接口