随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Android模仿Retrofit的建造者模式

一、概述

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

二、实例代码

  相关类介绍:

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

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

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

  Book.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
        }
    }
 
}

  测试方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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() + "");
 
    }

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

1
2
3
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框架,心血来潮模仿一个建造者模式

 

posted on   飘杨......  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示