适配器模式(一)

单项适配器模式:
原接口:

package com.jerry.design.adapter1.imp;


public interface InterfaceA {

public void testA();


}


接口实现类:

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!");

}


}


目标接口:

package com.jerry.design.adapter1.imp;


public interface InterfaceB {

public void testB();


}


目标接口实现类:


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!

}


}


总结:原接口转换为目标接口


posted on 2012-02-15 21:26 疯狂的蜗牛 阅读(54) 评论(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);

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

最新评论