美文网首页
安卓架构指南

安卓架构指南

作者: xiaobinZh | 来源:发表于2016-01-15 14:54 被阅读166次

Architecture Guidelines

安卓架构指南

The architecture of our Android apps is based on the MVP (Model View Presenter) pattern.
本指南基于MVP

  • View (UI layer): this is where Activities, Fragments and other standard Android components live. It's responsible for displaying the data received from the presenters to the user. It also handles user interactions and inputs (click listeners, etc) and triggers the right action in the Presenter if needed.
    (View UI层,Activities, Fragments 和其他安卓主机在这层,它负责显示从presenters层 传递给User的数据,它同时处理用户的交互和输入(点击监听等)和触发Presenter层的正确动作)

  • Presenter: presenters subscribe to RxJava Observables provided by the DataManager. They are in charge of handling the subscription lifecycle, analysing/modifying the data returned by the DataManager and calling the appropriate methods in the View in order to display the data.
    (presenters 订阅 RxJava 的可观察对象(有DataManager提供)。他们负责处理订阅的 生命周期,分析/修改数据(有DataManager 返回)同时调用View层合适的方法来显示数据)

  • Model (Data Layer): this is responsible for retrieving, saving, caching and massaging data. It can communicate with local databases and other data stores as well as with restful APIs or third party SDKs. It is divided in two parts: a group of helpers and a DataManager. The number of helpers vary between project and each of them has a very specific function, e.g. talking to an API or saving data in SharedPreferences. The DataManager combines and transforms the outputs from different helpers using Rx operators so it can: 1) provide meaningful data to the Presenter, 2) group actions that will always happen together. This layer also contains the actual model classes that define how the data structure is.
    (Model 层 负责检索,保存,缓存和整理数据,它负责建立和本地数据库,其他的数据储存方式(RESTFul API 或者 第三方SDK等等)的通信,它分割成两个部分,一个DataManager 和一组Helper类,Helper类的数量不定,有工程和特定的功能决定,比如:调用一个API或者保存数据在 SharedPreference。 DataManager 使用 Rx 操作符,来组合,转化不同Helper的输出)。因此他可以:1) 提供有意义的数据给Presenter层,2)组内的活动总是 同时 发生。本层同时包含实际的Model类,用于定义数据结构。

Looking at the diagram from right to left:

  • Helpers (Model): A set of classes, each of them with a very specific responsibility. Their function can range from talking to APIs or a database to implementing some specific business logic. Every project will have different helpers but the most common ones are:
    (一组类,每个类有特定的功能,功能可能包括 调用API(数据库)到 实现一些特定的 商业逻辑。每个工程 有不同的Helper类但是通用的是这些:)

    • DatabaseHelper: It handles inserting, updating and retrieving data from a local SQLite database. Its methods return Rx Observables that emit plain java objects (models)
      (处理插入,更新,索引本地SQLite 数据库。方法返回 Rx 可观察对象(发射普通的Java对象))
    • PreferencesHelper: It saves and gets data from SharedPreferences, it can return Observables or plain java objects directly.
      (这个类保存和获取数据从 SharedPreferences,可以直接返回 可观察者或者Java 对象)
    • Retrofit services : Retrofit interfaces that talk to Restful APIs, each different API will have its own Retrofit service. They return Rx Observables.
      (Retrofit 接口 可以 与RestFul API 通信,每一个不同的API 有各自的Retrofit 服务,他们可以返回 Rx 可观察对象)
  • Data Manager (Model): It's a key part of the architecture. It keeps a reference to every helper class and uses them to satisfy the requests coming from the presenters. Its methods make extensive use of Rx operators to combine, transform or filter the output coming from the helpers in order to generate the desired output ready for the Presenters. It returns observables that emit data models.
    (这是架构的关键部分,他保存每一个Helper类的引用和使用他们来满足Presenter 层的请求。他的方法是充分利用 Rx 操作 在 组合 转化,变换来自 Helper类的输出,来生成Presenter层想要的输出。他返回 可观察对象(发射数据 Model)

  • Presenters: Subscribe to observables provided by the DataManager and process the data in order to call the right method in the View.

  • Activities, Fragments, ViewGroups (View): Standard Android components that implement a set of methods that the Presenters can call. They also handle user interactions such as clicks and act accordingly by calling the appropriate method in the Presenter. These components also implement framework-related tasks such us managing the Android lifecycle, inflating views, etc.

  • Event Bus: It allows the View components to be notified of certain types of events that happen in the Model. Generally the DataManager posts events which can then be subscribed to by Activities and Fragments. The event bus is only used for very specific actions that are not related to only one screen and have a broadcasting nature, e.g. the user has signed out.
    (他允许 视图组件接收来自特定类型事件的通知(在Mode层产生),通常 DataManger 发射 的事件,可被 Activity和Fragment订阅。事件总线(用于非常具体的动作)不是只跟一个页面关联,他具有广播的自然属性,比如:用户登出)

相关文章

网友评论

      本文标题:安卓架构指南

      本文链接:https://www.haomeiwen.com/subject/qnehkttx.html