通过应用软件架构模式开发android应用程序始终是开发人员的首选。架构模式为项目文件提供了模块化,并确保所有代码都包含在单元测试中。它使开发人员将来可以轻松完成维护软件和扩展应用程序功能的任务。 MVP(模型-视图-演示者)和模型-视图-ViewModel(MVVM)是开发人员中最受欢迎的两种业界公认的android体系结构。
模型—视图—演示者(MVP)模式
MVP体系结构模式提供了一种简单的方法来构建项目代码。 MVP之所以被广泛接受,是因为它提供了模块化,可测试性以及更干净和可维护的代码库。它由以下三个组件组成:
- 型号:用于存储数据的层。它负责处理域逻辑(实际业务规则)以及与数据库和网络层的通信。
- 查看: UI(用户界面)层。它提供数据的可视化并跟踪用户的操作,以便通知Presenter。
- 演示者:从模型中获取数据并应用UI逻辑来决定要显示的内容。它管理视图的状态,并根据用户从视图中输入的通知执行操作。
模型—视图— ViewModel(MVVM)模式
MVVM模式与MVP(Model — View — Presenter)设计模式有一些相似之处,因为Presenter角色是由ViewModel扮演的。但是,MVVM解决了MVP模式的缺点。它建议将数据表示逻辑(视图或UI)与应用程序的核心业务逻辑部分分开。 MVVM的单独代码层是:
- 模型:该层负责数据源的抽象。 Model和ViewModel一起工作以获取和保存数据。
- 视图:该层的目的是向ViewModel通知用户的操作。该层遵守ViewModel,不包含任何类型的应用程序逻辑。
- ViewModel:它公开了与View相关的那些数据流。而且,它作为模型与视图之间的链接而充当服务器。
MVP和MVVM设计模式之间的差异
MVP(Model View Presenter) |
MVVM(Model View ViewModel) |
---|---|
It resolves the problem of having a dependent View by using Presenter as a communication channel between Model and View. | This architecture pattern is more event-driven as it uses data binding and thus makes easy separation of core business logic from the View. |
The one-to-one relationship exists between the Presenter and the View. | Multiple View can be mapped with single ViewModel. |
The Presenter has knowledge about the View. | ViewModel has no reference to the View. |
Model layer returns the response of the user’s input to the Presenter which forwards it to View. | After performing operations according to the user’s input, the Model layer returns the response to the View. |
Presenter handles the application flow and the View is the actual application. | ViewModel is the actual application and View is the interface for the user in order to interact with the app. |
The project file will contain more classes as well as code. | The Project file will contain more classes but less code per class. |
Ideal for simple and complex applications. | Not ideal for small scale projects. |
Easy to carry out Unit testing but a tight bond of View and Presenter can make it slightly difficult. | Unit testability is highest in this architecture. |
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!