• 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:不太利于代码调试  

  • 相关阅读:
    vim 查找选中文本_Chris!_百度空间
    export.py
    string.clear() liuyy的专栏 博客频道 CSDN.NET
    python判断有中文字符的方法
    vi技巧 ArduousBonze的专栏 博客频道 CSDN.NET
    cookies可以跨域了~单点登陆(a.com.cn与b.com.cn可以共享cookies)
    XML遍历(LINQ TO XML的魅力)
    当你使用LINQ做底层时,最好设计一个工厂,不要把LINQ的动作暴露给业务层
    EXCEL中如果输入一个数,然后自动让它乘以某个常数(第一列乘6,第二列乘4)
    面向对象~程序应该具有可维护性,代码可复用性,可扩展性和灵活性
  • 原文地址:https://www.cnblogs.com/whoislcj/p/5805921.html
Copyright © 2020-2023  润新知