适配器模式(二)

@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!
}

}

总结:正向反向均可适配。

 


posted on 2012-02-19 20:53 疯狂的蜗牛 阅读(73) 评论(0)  编辑  收藏 所属分类: 设计模式


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

公告

@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

最新评论