安卓架构指南

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订阅。事件总线(用于非常具体的动作)不是只跟一个页面关联,他具有广播的自然属性,比如:用户登出)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,794评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,050评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,587评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,861评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,901评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,898评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,832评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,617评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,077评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,349评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,483评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,199评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,824评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,442评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,632评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,474评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,393评论 2 352

推荐阅读更多精彩内容