菜鸟硕枫设计模式系列之7 适配器模式
发布日期:2021-10-01 08:45:07
浏览次数:4
分类:技术文章
本文共 1322 字,大约阅读时间需要 4 分钟。
适配器模式,正如适配器这个名字一样,起一个转换的作用。目的是通过接口转化,使得新系统和老系统可以正常交互。适配器模式是一种结构型模式。 适配器模式类图: [img]http://dl.iteye.com/upload/attachment/364532/87dc220e-6d9c-34b2-9864-bed07b82adcf.jpg[/img] 具体实现demo: 新系统:package adapterPattern; public interface NewSystem { public void doAnotherthing(String input); }实现:
package adapterPattern; public class NewSystemImpl implements NewSystem{ public void doAnotherthing(String input) { System.out.println("接口参数转换后,我能使用了哈"); } }老系统:
package adapterPattern; public interface OldSystem { public int doSomething(); }实现:
package adapterPattern; public class OldSystemImpl implements OldSystem{ public int doSomething() { System.out.println("我只能返回整型值"); return 0; } }适配器类:
package adapterPattern; public class Adapter { public String convertMethod(int input){ return Integer.toString(input); } }测试类:
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这个类就是适配器。
转载地址:https://blog.csdn.net/iteye_2655/article/details/81970363 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月27日 19时50分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
编程机制
2019-04-26
自己写的Java版计算器
2019-04-26
字、位、字节摘抄的,怕忘了
2019-04-26
printf与scanf的用法知识(C Primer Plus总结)
2019-04-26
三目运算符(条件运算符)
2019-04-26
C语言中的goto语句
2019-04-26
欧几里德算法及拓展
2019-04-26
CSDN-markdown编辑器基本用法
2019-04-26
等差数列公式搜集
2019-04-26
复合字面量(compound literal)
2019-04-26
gets和puts
2019-04-26
fgets、gets_s和fputs
2019-04-26
解决centos7联网问题
2019-04-26
c字符串函数
2019-04-26
nyoj1328派队方案
2019-04-26
nyoj322Sort归并排序
2019-04-26
nyoj1235A/B Problem逆元
2019-04-26
nyoj1270 care for China记忆化搜索
2019-04-26
linux常用命令
2019-04-26
快速排序qsort
2019-04-26