@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
接口A:
package com.jerry.design.adapter2.imp;
public interface InterfaceA {
public void testA();
}
接口B:
package com.jerry.design.adapter2.imp;
public interface InterfaceB {
public void testB();
}
实现类A:
package com.jerry.design.adapter2.impl;
import com.jerry.design.adapter2.imp.InterfaceA;
public class ImplA implements InterfaceA{
@Override
public void testA() {
System.out.println(" i am do something as InterfaceA!");
}
}
实现类B:
package com.jerry.design.adapter2.impl;
import com.jerry.design.adapter2.imp.InterfaceB;
public class ImplB implements InterfaceB{
@Override
public void testB() {
System.out.println(" i am do something as InterfaceB!");
}
}
实现类C:
package com.jerry.design.adapter2.impl;
import com.jerry.design.adapter2.imp.InterfaceA;
import com.jerry.design.adapter2.imp.InterfaceB;
public class ImplC implements InterfaceB,InterfaceA{
private InterfaceA implA;
private InterfaceB implB;
public ImplC(InterfaceA implA,InterfaceB implB){
this.implA = implA;
this.implB = implB;
}
@Override
public void testB() {
implA.testA();
}
@Override
public void testA() {
implB.testB();
}
}
测试类:
package com.jerry.design.adapter2.client;
import com.jerry.design.adapter2.imp.InterfaceA;
import com.jerry.design.adapter2.imp.InterfaceB;
import com.jerry.design.adapter2.impl.ImplA;
import com.jerry.design.adapter2.impl.ImplB;
import com.jerry.design.adapter2.impl.ImplC;
public class Test {
public static void main(String[] args) {
InterfaceA implA = (InterfaceA) new ImplA();
InterfaceB implB = (InterfaceB) new ImplB();
InterfaceA implC = (InterfaceA) new ImplC(implA,implB);
InterfaceB implD = (InterfaceB) new ImplC(implA,implB);
implC.testA();// i am do something as InterfaceB!
implD.testB();// i am do something as InterfaceA!
}
}
总结:正向反向均可适配。