Android總結之鏈式呼叫(方法鏈)

總李寫程式碼發表於2016-08-26

前言:

     最近在學習總結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:不太利於程式碼除錯  

相關文章