国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android總結之鏈式調用(方法鏈)

2019-11-09 18:03:23
字體:
來源:轉載
供稿:網友

前言:

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东宁县| 当涂县| 临夏市| 汪清县| 县级市| 庆云县| 怀仁县| 黄骅市| 万山特区| 舞钢市| 郎溪县| 扎赉特旗| 马龙县| 旬邑县| 吴桥县| 集贤县| 盈江县| 迁西县| 汉源县| 毕节市| 广元市| 马鞍山市| 宁夏| 崇明县| 东港市| 陆良县| 深州市| 荆门市| 涟源市| 靖远县| 民权县| 贡觉县| 新河县| 当涂县| 鄂伦春自治旗| 安岳县| 高清| 夹江县| 黄龙县| 北宁市| 保山市|