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;//消息Id
private 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;//消息Id
private 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总结之链式调用(方法链)的更多相关文章
- javascript方法链式调用和构造函数链式调用对比
先说一下方法链:B的实例从A继承了A中的同名方法,如果B的方法重载了A中的方法,B中的重载方法可能会调用A中的重载方法,这种方法称为方法链. 构造函数链:子类的构造函数B()有时需要调用父类的构造函数 ...
- 在 iOS 中实现方法链调用
编译:伯乐在线 - 林欣达 如有好文章投稿,请点击 → 这里了解详情 如需转载,发送「转载」二字查看说明 前言 链式调用(chained calls)是指在函数调用返回了一个对象的时候,使得这个调用链 ...
- Android HTTP实例 使用GET方法和POST方法发送请求
Android HTTP实例 使用GET方法和POST方法发送请求 Web程序:使用GET和POST方法发送请求 首先利用MyEclispe+Tomcat写好一个Web程序,实现的功能就是提交用户信息 ...
- [Effective JavaScript 笔记]第60条:支持方法链
无状态的API的部分能力是将复杂操作分解为更小的操作的灵活性.一个很好的例子是字符串的replace方法.由于结果本身也是字符串,可以对前一个replace操作重复执行替换.这种模式的一个常见用例是在 ...
- ES6 Promise对象then方法链式调用
then()方法的作用是Promise实例添加解决(fulfillment)和拒绝(rejection)状态的回调函数.then()方法会返回一个新的Promise实例,所以then()方法后面可以继 ...
- 测开之路一百零一:jquery文字特效、动画、方法链
文字特效 html内容 1.卷起/展开 2.隐藏/显示 3.淡入淡出 <!DOCTYPE html><html lang="en"><head> ...
- php对象方法链式调用编程
E:\html\tproject\framework\modules\common\classes\Common\CURL.php <?php /** * 同步发起请求 * 针对http协议的8 ...
- [Android] Android统计Apk , jar包方法数
reference to : http://www.jianshu.com/p/61e8f803e0d1 Android在开发过程中,随着引用的库以及业务的增多,不可避免的会出现64K limit问题 ...
- android intent隐式调用之一个应用程序启动另一个应用程序
理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似:另一种是隐式的Intent,即Inten ...
随机推荐
- 试试SQLSERVER2014的内存优化表
试试SQLSERVER2014的内存优化表 SQL Server 2014中的内存引擎(代号为Hekaton)将OLTP提升到了新的高度. 现在,存储引擎已整合进当前的数据库管理系统,而使用先进内存技 ...
- 使用 .NET WinForm 开发所见即所得的 IDE 开发环境,实现不写代码直接生成应用程序
直接切入正题,这是我09年到11年左右业余时间编写的项目,最初的想法很简单,做一个能拖拖拽拽就直接生成应用程序的工具,不用写代码,把能想到的业务操作全部封装起来,通过配置的方式把这些业务操作组织起来运 ...
- [原] KVM 虚拟化原理探究(6)— 块设备IO虚拟化
KVM 虚拟化原理探究(6)- 块设备IO虚拟化 标签(空格分隔): KVM [toc] 块设备IO虚拟化简介 上一篇文章讲到了网络IO虚拟化,作为另外一个重要的虚拟化资源,块设备IO的虚拟化也是同样 ...
- 分页插件--根据Bootstrap Paginator改写的js插件
刚刚出来实习,之前实习的公司有一个分页插件,和后端的数据字典约定好了的,基本上是看不到内部是怎么实现的,新公司是做WPF的,好像对于ASP.NET的东西不多,导师扔了一个小系统给我和另一个同事,指了两 ...
- ES6(块级作用域)
我们都知道在javascript里是没有块级作用域的,而ES6添加了块级作用域,块级作用域能带来什么好处呢?为什么会添加这个功能呢?那就得了解ES5没有块级作用域时出现了哪些问题. ES5在没有块级作 ...
- myrocks复制中断问题排查
背景 mysql可以支持多种不同的存储引擎,innodb由于其高效的读写性能,并且支持事务特性,使得它成为mysql存储引擎的代名词,使用非常广泛.随着SSD逐渐普及,硬件存储成本越来越高,面向写优化 ...
- [Hadoop in Action] 第7章 细则手册
向任务传递定制参数 获取任务待定的信息 生成多个输出 与关系数据库交互 让输出做全局排序 1.向任务传递作业定制的参数 在编写Mapper和Reducer时,通常会想让一些地方可以配 ...
- Linux服务器安全配置
众所周知,网络安全是一个非常重要的课题,而服务器是网络安全中最关键的环节.Linux被认为是一个比较安全的Internet服务器,作为一种开放源代码操作系统,一旦Linux系统中发现有安全漏洞,Int ...
- codevs 3289 花匠
题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...
- Storm介绍(一)
作者:Jack47 PS:如果喜欢我写的文章,欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 内容简介 本文是Storm系列之一,介绍了Storm的起源,Storm ...