自定义视图库 Android 的架构 [英] Architecture for a Custom View library Android

查看:21
本文介绍了自定义视图库 Android 的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个库,每 30 秒显示一个问题(从 REST Api 获取)并允许用户选择一个可能的答案.

I am building a library that every 30 seconds displays a question (obtained from a REST Api) and allows the user to select one of the possible answers.

此外,我需要在应用中使用该库,在问题下方显示视频.

Also, I need to make use of that library in an app, displaying a video underneath the question.

预期结果

所有的业务和 UI 逻辑都应该在库中处理.

All the business and UI logic should be handled in the library.

使用 MVVM 方法和库中的存储库模式有意义吗?用这个包结构?

Does it make sense to use an MVVM approach, with repository pattern in the library? With this package structure?

可能的包结构

推荐答案

您正在创建库,与应用程序相比,这是完全不同的上下文和创建代码库的方式.

You're creating the library, which is totally different context and way of creating the codebase comparing to the app.

首先,您需要对依赖项做出决定.当然,将改造、mvvm 和其他依赖项放入应用程序并进一步管理它们是非常容易的.

First of all you need to make decisions about the dependencies. Of course it is super easy to put retrofit, mvvm and other dependencies into the app and manage them further.

有了图书馆就没有那么容易了.首先,我对库的期望是尽可能少的依赖.如果您要提供 aar 文件,则开发人员必须在项目中包含这些依赖项.另一方面,如果您将库发布到 maven 存储库,gradle 将处理库的依赖项.然而,这里最大的挑战是维护库和解决使用库的实际应用程序中的冲突.这可能会导致沮丧,最终有人会将您的库扔掉,从库创建者的角度来看,这是最糟糕的事情.

With library it's not that easy. What I would expect from library in the first place is to have as little dependencies as possible. If you're going to provide aar file, then the developer would have to include those dependencies in the project. On the other hand, if you're publishing the library to maven repo, the gradle will take care of the library's dependencies. However the biggest challenge here is maintaining the library and resolving conflicts in actual app that uses the library. It can lead to frustration and eventually someone can throw your library away, which is the worst thing from library creator's perspective.

我对这个小型库的建议(调用 api 并向自定义视图提供数据)是:

What I would suggest for this small library (calling api and providing data to custom view) is to:

  1. 仅使用 OkHttp 而不进行改造以提供来自 REST api 的数据.
  2. 如果是库的情况,请使用公共函数/方法在 OkHttp 客户端上创建一些抽象,以便开发人员可以自己获取数据.创建这种界面的一种很酷的方法是使用 Fluent Interface(例如,如果你想要库的客户端将他们的访问令牌/密钥放在 REST api 中,以检测谁在调用 api、调用了多少、限制了他们的请求等)
  3. 如果您还想提供自定义视图,我建议三思而后行,将 REST 客户端和自定义视图本身分开,让开发人员自己放置数据.另一方面,如果你想自己处理它,你必须考虑错误处理、状态管理、取消请求的生命周期回调等.你可以为开发人员提供一些监听器接口,让他们知道自定义中发生了什么查看.
  1. Use only OkHttp without retrofit to provide the data from the REST api.
  2. If it's the case for the library, create some abstraction over the OkHttp client with public functions/methods so the developers can get the data by themselves. Cool approach for creating this kind of interface is to use Fluent Interface (for example if you want the client of the library to put their access token/secret key for the REST api to detect who is calling the api, how much, limit their requests etc)
  3. If you want to also provide the Custom View I would suggest to think twice about separating the REST client and Custom View itself and let the developer put the data by themselves. On the other hand if you want to handle it by yourself, you have to consider error handling, state management, lifecycle callbacks for cancelling the requests etc. You can provide some Listener interfaces for the developers so they know what is going on in the custom view.

构建库的方式取决于您,如果您只有一个依赖项(其余客户端),只需通过构造函数传递对象,这样您就无需添加另一个依赖项.或者只是简单地使用 ServiceLocator 模式,但是它也应该在设置库时连接到应用程序/活动本身,以便正确销毁对象

The way you structure the library is up to you, if you have only one dependency (the rest client) just pass the objects via constructor so you won't need to add another dependency. Or just simply use ServiceLocator pattern, however it should also be connected to the Application/Activity itself when setting up the library so the objects are destroyed properly

这篇关于自定义视图库 Android 的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆