博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 适配器模式
阅读量:7088 次
发布时间:2019-06-28

本文共 2180 字,大约阅读时间需要 7 分钟。

适配器:基于现有类所提供的服务,向客户提供接口,以满足客户的期望

                                                       《Java设计模式》

一、类适配器:

OtherOperation(已存在所需功能的类):

/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:39:18 */public class OtherOperation {    public int add(int a, int b){        return a + b;    }}

Operation(为所要实现的功能定义接口):

/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:40:12 */public interface Operation {    public int add(int a, int b);}

OperationAdapter(适配器):

/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:40:41 * 对象适配器 */public class OperationAdapter extends OtherOperation implements Operation{}

-----------------------------------------------------------------------------------------------------------------------------------

二、对象适配器:

假如客户接口期望的功能不止一个,而是多个。

由于java是不能实现多继承的,所以我们不能通过构建一个适配器,让他来继承所有原以完成我们的期望,这时候怎么办呢?只能用适配器的另一种实现--对象适配器:
符合java提倡的编程思想之一,即尽量使用聚合不要使用继承。

OtherAdd和OtherMinus(已存在功能的两个类):

/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:50:14 */public class OtherAdd {    public int add(int a, int b){        return a + b;    }}
/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:50:46 */public class OtherMinus {    public int minus(int a, int b){        return a - b;    }}

Operation(为所要实现的功能定义接口):

/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:51:59 */public interface Operation {    public int add(int a, int b);    public int minus(int a, int b);}

OperationAdapter(通过适配类的对象来获取):

/** * @author com.tiantian * @version 创建时间:2012-11-21 下午4:52:36 */public class OperationAdapter implements Operation{    private OtherAdd otherAdd;    private OtherMinus otherMinus;        public OtherAdd getOtherAdd() {        return otherAdd;    }    public void setOtherAdd(OtherAdd otherAdd) {        this.otherAdd = otherAdd;    }    public OtherMinus getOtherMinus() {        return otherMinus;    }    public void setOtherMinus(OtherMinus otherMinus) {        this.otherMinus = otherMinus;    }    @Override    public int add(int a, int b) {        return otherAdd.add(a, b);    }    @Override    public int minus(int a, int b) {        return otherMinus.minus(a, b);    }}
本文转自天天_byconan博客园博客,原文链接:http://www.cnblogs.com/tiantianbyconan/archive/2012/11/21/2781050.html
,如需转载请自行联系原作者
你可能感兴趣的文章
怎样调通微信支付及微信发货通知接口(Js API)
查看>>
Android 属性动画(Property Animation) 全然解析 (下)
查看>>
推断汉字正則表達式更严谨方法!
查看>>
如何避免误删CleanMyMac语言文件
查看>>
Linux下免安装mysql
查看>>
快钱报错:javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name解决
查看>>
Hadoop集群WordCount运行详解(转)
查看>>
[转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
查看>>
一次性搞清楚equals和hashCode
查看>>
Android Studio IDE的 LogCat如何过滤指定应用的调试信息
查看>>
23个常用正则表达式(数值和字符串)
查看>>
struts2中struts.xml配置文件详解
查看>>
Javascript中的with用法
查看>>
GIS-008-ArcGIS JS API 全图
查看>>
js splice方法
查看>>
Linux--多网卡的7种Bond模式
查看>>
ADO 连接数据库,取到VT_DATE型日期转换成 int型
查看>>
properties 配置文件中值换行的问题
查看>>
Azure 部署 Asp.NET Core Web App
查看>>
Masonry和FDTemplateLayoutCell 结合使用示例Demo
查看>>