Android总结之链式调用(方法链)
前言:
最近在学习总结Android属性动画的时候,发现Android的属性动画设计采用了链式调用的方式,然后又回顾了一下了以前接触的开源框架Glide也是采用链式调用的方式,还有最近火的一塌糊涂的RxJava也是采用链式调用,为何如此之多的开源项目采用这种设计方式,今天来对比学习一下。
什么是链式调用?
链式调用其实只不过是一种语法招数。它能让你通过重用一个初始操作来达到用少量代码表达复杂操作的目的。
表现形式:
一个初始化操作之后,后面的调用以“.”连接起来。例如Glide使用
Glide.with(this).load(imageUrl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
实际举例:
以以前做的简单的IM即时通讯消息体MsgInfo为例。
1.)普通实现方式
MsgInfo.java实现方式
public class MsgInfo {/*** 消息的类型*/public static class Type {public final static int TEXT = 0; // 文本消息public final static int IMAGE = 1; // 图片消息public final static int VOICE = 2; // 语音消息public final static int MOVIE = 3;// 视频消息public final static int URL = 4;//URL消息}/*** 消息的方向*/public static class Direct {public final static int SEND = 0; // 发送public final static int RECEIVE = 1; // 接收}/*** 消息的状态*/public static class Status {public final static int SEND_SUCCESS= 0; // 已发送public final static int SENDING = 1; // 正在发送public final static int SEND_FAILED = 2; // 发送失败public final static int READ = 3; // 已读public final static int UNREAD = 4; // 未读}private long msgId;//消息Idprivate String ownerId;//消息属于哪个用户private String relatedId;//消息关联到哪个用户;private String body;//消息体private long time;//消息发送接收时间private int direct;// 消息的方向private int status;//消息的状态private int type;//消息的类型public MsgInfo() {}public long getMsgId() {return msgId;}public void setMsgId(long msgId) {this.msgId = msgId;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getOwnerId() {return ownerId;}public void setOwnerId(String ownerId) {this.ownerId = ownerId;}public String getRelatedId() {return relatedId;}public void setRelatedId(String relatedId) {this.relatedId = relatedId;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public long getTime() {return time;}public void setTime(long time) {this.time = time;}public int getDirect() {return direct;}public void setDirect(int direct) {this.direct = direct;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;} }
调用方式
MsgInfo msgInfo = new MsgInfo(); msgInfo.setOwnerId("100011002"); msgInfo.setRelatedId("1000110003"); msgInfo.setBody("hello 普通调用"); msgInfo.setType(MsgInfo.Type.TEXT); msgInfo.setDirect(MsgInfo.Direct.SEND); msgInfo.setStatus(MsgInfo.Status.SENDING); msgInfo.setTime(System.currentTimeMillis());
2.)链式调用方式
MsgInfo.java实现
public class MsgInfo {/*** 消息的类型*/public static class Type {public final static int TEXT = 0; // 文本消息public final static int IMAGE = 1; // 图片消息public final static int VOICE = 2; // 语音消息public final static int MOVIE = 3;// 视频消息public final static int URL = 4;//URL消息}/*** 消息的方向*/public static class Direct {public final static int SEND = 0; // 发送public final static int RECEIVE = 1; // 接收}/*** 消息的状态*/public static class Status {public final static int SEND_SUCCESS= 0; // 已发送public final static int SENDING = 1; // 正在发送public final static int SEND_FAILED = 2; // 发送失败public final static int READ = 3; // 已读public final static int UNREAD = 4; // 未读}private long msgId;//消息Idprivate String ownerId;//消息属于哪个用户private String relatedId;//消息关联到哪个用户;private String body;//消息体private long time;//消息发送接收时间private int direct;// 消息的方向private int status;//消息的状态private int type;//消息的类型public MsgInfo() {}public long getMsgId() {return msgId;}public MsgInfo setMsgId(long msgId) {this.msgId = msgId;return this;}public int getType() {return type;}public MsgInfo setType(int type) {this.type = type;return this;}public String getOwnerId() {return ownerId;}public MsgInfo setOwnerId(String ownerId) {this.ownerId = ownerId;return this;}public String getRelatedId() {return relatedId;}public MsgInfo setRelatedId(String relatedId) {this.relatedId = relatedId;return this;}public String getBody() {return body;}public MsgInfo setBody(String body) {this.body = body;return this;}public long getTime() {return time;}public MsgInfo setTime(long time) {this.time = time;return this;}public int getDirect() {return direct;}public MsgInfo setDirect(int direct) {this.direct = direct;return this;}public int getStatus() {return status;}public MsgInfo setStatus(int status) {this.status = status;return this;} }
调用方式
MsgInfo msgInfo = new MsgInfo();msgInfo.setOwnerId("100011002").setRelatedId("1000110003").setBody("hello 链式调用").setType(MsgInfo.Type.TEXT).setDirect(MsgInfo.Direct.SEND).setStatus(MsgInfo.Status.SENDING).setTime(System.currentTimeMillis());
3.)对比两者优劣
普通:
1:维护性强
2:对方法的返回类型无要求
3:对程序员的业务要求适中
链式:
1:编程性强
2:可读性强
3:代码简洁
4:对程序员的业务能力要求高
5:不太利于代码调试
干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!
Android总结之链式调用(方法链)相关推荐
- Python编程基础:第四十五节 方法链Method Chaining
第四十五节 方法链Method Chaining 前言 实践 前言 方法链是指一个对象一次调用其自身的多个方法,通常写作对象.方法1.方法2.由于这种调用方法看起来像一个链条,所以我们将其称作方法链. ...
- sklearn朴素贝叶斯分类器_python机器学习:方法链和朴素贝叶斯分类器
今天我们在学习朴素贝叶斯分类器之前,我们先来总结下前面经常用到的内容,方法链:在scikit-learn中所有模型的fit方法返回的都是self.我们用一行代码初始化模型并拟合,对应代码如下:logr ...
- 链式调用方法的实现原理和方法
1.什么是链式调用? Person person = new Person().setName(fog).setAge(18).setSex(man).setJob(software engineer ...
- 深入理解python.md_跟黄哥学python序列文章之python方法链(method chaining)
跟黄哥学python序列文章之python方法链(method chaining) 写这篇文章来由,有朋友说下面这样的代码看不懂. choice = raw_input("please in ...
- jQuery方法链式调用的原理
在使用jQuery库的时候,是可以连续调用多个方法的,这是怎么实现的呢?如: $("div").width("100px").height("100p ...
- [Android] Android统计Apk , jar包方法数
reference to : http://www.jianshu.com/p/61e8f803e0d1 Android在开发过程中,随着引用的库以及业务的增多,不可避免的会出现64K limit问题 ...
- Android Handler中的handleMessage方法和post方法之源码剖析
我们都知道,在子线程中进行UI操作(更新UI控件)包括以下四种方法: 1.Handler的handlerMessage()方法. 2.Handler的post()方法. 3.View的post()方法 ...
- android 如何添加第3方lib库到kernel中
注意:只能将lib库放在kernel编译到的地方,如下: alps/kernel/ alps/mediatek/custom/common/kernel/ alps/mediatek/custom/$ ...
- 关于Android中TextView的setText方法报错
当Android报错如下: Process: com.qiang.sifter, PID: 4759 android.content.res.Resources$NotFoundException: ...
最新文章
- IJCAI 2020灭霸式拒稿,AI审稿是否更公平?
- python使用matplotlib可视化、为可视化图像添加图例(legend)、自定义图例的字体格式、字体大小、字体颜色等
- 送书福利 | 浙江大学陈华钧教授新作,全面梳理知识图谱技术体系
- Python常见数据结构整理
- [BUUCTF-pwn]——picoctf_2018_are you root
- java 调 cmd 没反应
- c语言 变量 定义 使用,C语言为什么要规定对所用到的变量要“先定义,后使用”...
- Python基础(12)--模块
- 利用NSight进行交叉编译
- jenkins构建后脚本不执行_接口管理工具ApiPost-预(后)执行脚本常用方法集合
- 2022牛客寒假算法基础集训营2 签到题7题
- shiro+springmvc+mybatis【转】
- mysql MyISAM和InnoDB
- 号称是世界最大的电子书图书馆ZLibrary
- Excel最强玩法!用Excel做“动态日历表”,让你的好记性更牢固!
- 一个JSP页面打开另外一个JSP页面并传值
- php 2038年,PHP 处理大于2038年以后的日期
- ele表格合并行之后的selection选中
- JNA二次开发华视身份证阅读器
- 代谢组学通路富集分析
热门文章
- vue 监听表格里的数据变化_vue中监听数据变化 watch
- 计算机维修实训室制度,计算机实训室规章制度
- dataframe python格式_python3.6 pandas,Series和DataFrame基础格式与用法,附代码实例
- dataframe两个表合并_史上代码最少的工作表拆分,仅需5行,不可思议
- 15、如何在Linux和Windows下清除DNS缓存
- 解决Could not open requirements file: [Errno 2] No such file or directory: ‘requirements.txt‘问题
- 鼠标事件响应函数之DOWN:画红色的点
- ads无法启用状态服务器,NAC ADSSO 无法工作在Microsoft 2008服务器版本
- ConcurrentHashMap源码解析(1)
- Python Singleton模式