前言:
最近在學習總結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:不太利於代碼調試