首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

初学者硕枫设计模式系列之7 适配器模式

2012-09-22 
菜鸟硕枫设计模式系列之7 适配器模式适配器模式,正如适配器这个名字一样,起一个转换的作用。目的是通过接口

菜鸟硕枫设计模式系列之7 适配器模式
适配器模式,正如适配器这个名字一样,起一个转换的作用。目的是通过接口转化,使得新系统和老系统可以正常交互。适配器模式是一种结构型模式。

适配器模式类图:
                

具体实现demo:
新系统:

package adapterPattern;public class AdapterPatternTest{public static void main(String[] args){OldSystemImpl oldSystem = new OldSystemImpl();NewSystemImpl newSystemImpl = new NewSystemImpl();Adapter adapter = new Adapter();newSystemImpl.doAnotherthing(adapter.convertMethod(oldSystem.doSomething()));}}


说明:老系统参数为int ,新系统接收输入为string,接口不匹配,通过adapter的转化之后,使得新老系统可以交互了。另外,jdk 1.6中  Runable task 转成 Callable task就是一个典型的适配器模式,其中  RunnableAdapter这个类就是适配器。

热点排行