通过应用软件架构模式开发 android 应用程序始终是开发人员的首选。架构模式为项目文件提供了模块化,并确保所有代码都包含在单元测试中。它使开发人员可以轻松地维护软件并在未来扩展应用程序的功能。 MVC(模型 — 视图 — 控制器)和MVP(模型 — 视图 — 演示者) 是两个 开发人员中最流行的安卓架构。
模型—视图—控制器(MVC)模式
MVC 模式建议将代码拆分为 3 个组件。在创建应用程序的类/文件时,开发人员必须将其分类为以下三层之一:
模型:此组件存储应用程序数据。它对接口一无所知。该模型负责处理领域逻辑(现实世界的业务规则)以及与数据库和网络层的通信。
视图:它是 UI(用户界面)层,其中包含在屏幕上可见的组件。此外,它提供存储在模型中的数据的可视化,并为用户提供交互。
控制器:该组件建立视图和模型之间的关系。它包含核心应用程序逻辑并了解用户的行为并根据需要更新模型。
模型—视图—演示者(MVP)模式
MVP 模式克服了 MVC 的挑战,并提供了一种构建项目代码的简单方法。 MVP 被广泛接受的原因是它提供了模块化、可测试性以及更干净和可维护的代码库。它由以下三个部分组成:
模型:用于存储数据的层。它负责处理域逻辑(现实世界的业务规则)以及与数据库和网络层的通信。
视图: UI(用户界面)层。它提供数据的可视化并跟踪用户的操作以通知演示者。
Presenter:从模型中获取数据并应用 UI 逻辑来决定要显示的内容。它管理视图的状态并根据用户从视图输入的通知采取行动。
MVC 和 MVP 设计模式之间的主要区别
MVC(Model View Controller) |
MVP(Model View Presenter |
---|---|
One of the oldest software architecture | Developed as the second iteration of software architecture which is advance from MVC. |
UI(View) and data-access mechanism(Model) are tightly coupled. | The View is loosely coupled to the Model. |
Controller and View layer falls in the same activity/fragment | Communication between View-Presenter and Presenter-Model happens via an interface. |
User inputs are handled by Controller which instructs the model for further operations. | User inputs are handled by View which instructs the presenter to call appropriate functions. |
The many-to-one relationship exists between Controller and View as one Controller can select different View based upon required operations. | The one-to-one relationship exists between Presenter and View as one Presenter class manages one View at a time. |
The Controller is the overall in charge as it creates the appropriate View and interacts with the Model according to the user’s request. | The View is the overall in charge in this schema as View call methods of Presenter which further directs Model. |
Limited support to Unit Testing | Unit Testing is highly supported. |