• Android模仿Retrofit的建造者模式


    一、概述

      在Retrofit的框架中用的最多的是建造者模式,建造者模式对象与设置的值相分离层次结构更加清晰,在使用的时候通过链式调用赋值,层次清晰明了,避免了我们要new不多的对象需要构造多个构造函数,或者创建对象后一个个的赋值,非常的方便。下面我们也模仿一个建造者模式看看其神奇之处。

    二、实例代码

      相关类介绍:

      1.Book.java建造者中用到的实体

      2.MyRetrofit.java建造者是在此类中实现的

      3.测试方法以及打印日志

      Book.java

    package com.yw.rxjava3demo;
    
    import android.app.Service;
    
    import java.io.Serializable;
    
    import io.reactivex.Observable;
    import io.reactivex.Observer;
    
    /**
     * create by yangwei
     * on 2020-02-21 18:08
     */
    public class Book {
        private String id;
        private String name;
    
        public Book(String id, String name) {
            this.id = id;
            this.name = name;
    
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

      MyRetrofit.java

    package com.yw.rxjava3demo;
    
    import java.util.List;
    
    /**
     * 模仿Retrofit的建造者模式
     * create by yangwei
     * on 2020-02-26 11:15
     */
    public class MyRetrofit {
        private String name;
        private int age;
        private List<Book> datas;
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    
        public List<Book> getBooks() {
            return datas;
        }
    
        public MyRetrofit(String name, int age, List<Book> datas) {
    
            this.name = name;
            this.age = age;
            this.datas = datas;
        }
    
        public static final class Builder {
            private String name;
            private int age;
            private List<Book> datas;
    
            public Builder() {
            }
    
            public Builder setName(String name) {
                this.name = name;
                return this;
            }
    
            public Builder setAge(int age) {
                this.age = age;
                return this;
            }
    
            public Builder setList(List<Book> books) {
                this.datas = books;
                return this;
            }
    
            public MyRetrofit build() {
                return new MyRetrofit(this.name, this.age, this.datas);
            }
        }
    
    }
    

      测试方法:

    private void MyRetrofitBuildTest() {
            List<Book> datas = new ArrayList<>();
            datas.add(new Book("1", "《萌鸡小队》"));
            datas.add(new Book("2", "《小猪佩奇》"));
            MyRetrofit retrofit = new MyRetrofit.Builder()
                    .setName("杨洛峋")
                    .setAge(1)
                    .setList(datas)
                    .build();
    
            Log.e("姓名:", retrofit.getName());
            Log.e("年龄:", retrofit.getAge() + "");
            StringBuffer sb = new StringBuffer();
            for (Book book : retrofit.getBooks()) {
                sb.append(book.getName()).append(",");
            }
            Log.e("喜欢:", sb.toString() + "");
    
        }
    

      打印日志:通过建造者模式构建数据后会打印姓名、年龄、以及喜欢的动漫

    2020-02-26 11:29:20.312 32103-32103/? E/姓名:: 杨洛峋
    2020-02-26 11:29:20.312 32103-32103/? E/年龄:: 1
    2020-02-26 11:29:20.312 32103-32103/? E/喜欢:: 《萌鸡小队》,《小猪佩奇》,
    

      ps:好了,本文比较简单,纯粹是因为retrofit框架,心血来潮模仿一个建造者模式

  • 相关阅读:
    [LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符
    [LeetCode] 158. Read N Characters Given Read4 II
    AndroidManifest.xml文件详解(activity)(一)
    Android中Bitmap、Drawable、byte[]转换
    图片和byte[]数组互转
    EditText 属性
    adb server didn't ack failed to start daemon
    PhoneGap 获得设备属性Demo
    PhoneGap 第一个程序
    android手机常用分辨率
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/12365944.html
Copyright © 2020-2023  润新知