首页 > 其他分享 >软件体系架构方面英文文章翻译----Android中MVC、MVP和MVVM架构模式的区别

软件体系架构方面英文文章翻译----Android中MVC、MVP和MVVM架构模式的区别

时间:2023-03-02 20:13:38浏览次数:54  
标签:MVP 架构 MVVM ViewModel 视图 Model View

Difference Between MVC, MVP and MVVM Architecture Pattern in Android

22 Dec, 2022

Developing an android application by applying a software architecture pattern is always preferred by the developers. An architecture pattern gives modularity to the project files and assures that all the codes get covered in Unit testing. It makes the task easy for developers to maintain the software and to expand the features of the application in the future. MVC (Model — View — Controller), MVP (Model — View — Presenter), and MVVM (Model — View — ViewModel) is the most popular and industry-recognized android architecture pattern among developers.

The Model—View—Controller(MVC) Pattern

The MVC pattern suggests splitting the code into 3 components. While creating the class/file of the application, the developer must categorize it into one of the following three layers:

  • Model: This component stores the application data. It has no knowledge about the interface. The model is responsible for handling the domain logic(real-world business rules) and communication with the database and network layers.
  • View: It is the UI(User Interface) layer that holds components that are visible on the screen. Moreover, it provides the visualization of the data stored in the Model and offers interaction to the user.
  • Controller: This component establishes the relationship between the View and the Model. It contains the core application logic and gets informed of the user’s response and updates the Model as per the need.

The Model—View—Controller(MVC) Pattern

The Model—View—Presenter(MVP) Pattern

MVP pattern overcomes the challenges of MVC and provides an easy way to structure the project codes. The reason why MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase. It is composed of the following three components:

  • Model: Layer for storing data. It is responsible for handling the domain logic(real-world business rules) and communication with the database and network layers.
  • View: UI(User Interface) layer. It provides the visualization of the data and keep a track of the user’s action in order to notify the Presenter.
  • Presenter: Fetch the data from the model and applies the UI logic to decide what to display. It manages the state of the View and takes actions according to the user’s input notification from the View.

The Model—View—Presenter(MVP) Pattern

The Model — View — ViewModel (MVVM) Pattern

MVVM pattern has some similarities with the MVP(Model — View — Presenter) design pattern as the Presenter role is played by the ViewModel. However, the drawbacks of the MVP pattern has been solved by MVVM. It suggests separating the data presentation logic(Views or UI) from the core business logic part of the application. The separate code layers of MVVM are:

  • Model: This layer is responsible for the abstraction of the data sources. Model and ViewModel work together to get and save the data.
  • View: The purpose of this layer is to inform the ViewModel about the user’s action. This layer observes the ViewModel and does not contain any kind of application logic.
  • ViewModel: It exposes those data streams which are relevant to the View. Moreover, it servers as a link between the Model and the View.

The Model — View — ViewModel (MVVM) Pattern

Difference Between MVC, MVP, and MVVM Design Pattern

MVC(MODEL VIEW CONTROLLER) MVP(MODEL VIEW PRESENTER) MVVM(MODEL VIEW VIEWMODEL)
One of the oldest software architecture Developed as the second iteration of software architecture which is advance from MVC. Industry-recognized architecture pattern for applications.
UI(View) and data-access mechanism(Model) are tightly coupled. 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.
Controller and View exist with the one-to-many relationship. One Controller can select a different View based upon required operation. The one-to-one relationship exists between Presenter and View as one Presenter class manages one View at a time. Multiple View can be mapped with a single ViewModel and thus, the one-to-many relationship exists between View and ViewModel.
The View has no knowledge about the Controller. The View has references to the Presenter. The View has references to the ViewModel
Difficult to make changes and modify the app features as the code layers are tightly coupled. Code layers are loosely coupled and thus it is easy to carry out modifications/changes in the application code. Easy to make changes in the application. However, if data binding logic is too complex, it will be a little harder to debug the application.
User Inputs are handled by the Controller. The View is the entry point to the Application The View takes the input from the user and acts as the entry point of the application.
Ideal for small scale projects only. Ideal for simple and complex applications. Not ideal for small scale projects.
Limited support to Unit testing. 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.
This architecture has a high dependency on Android APIs. It has a low dependency on the Android APIs. Has low or no dependency on the Android APIs.
It does not follow the modular and single responsibility principle. Follows modular and single responsibility principle. Follows modular and single responsibility principle.

The key difference between MVP and MVC is that the Presenter in MVP has a more active role in the communication between the Model and the View, and is responsible for controlling the flow of data between the two. MVVM stands for Model-View-ViewModel. In the MVVM pattern, the “Model” represents the data and logic of the application, the “View” represents the UI of the application, and the “ViewModel” is a layer that sits between the Model and the View and is responsible for exposing the data and logic of the Model to the View in a way that is easier to work with. The ViewModel also handles user input and updates the Model as needed.

Overall, the main difference between these patterns is the role of the mediator component. MVC and MVP both involve a Controller or Presenter that acts as a mediator between the Model and the View, while MVVM involves a ViewModel that serves as the mediator between the Model and the View. MVC is the simplest of these patterns, while MVP and MVVM are more flexible and allow for a cleaner separation of concerns between the different layers of the application.


Android中MVC、MVP和MVVM架构模式的区别

22 Dec, 2022

通过应用软件架构模式来开发android应用程序,总是被开发人员所青睐。架构模式为项目文件提供了模块化,并确保所有的代码都被涵盖在单元测试中。它使开发人员能够轻松地维护软件,并在未来扩展应用程序的功能。MVC(模型-视图-控制器), MVP(模型-视图-展示者), 和MVVM(模型-视图-视图模型)是开发人员中最流行和业界公认的安卓架构模式

模型-视图-控制器(MVC)模式

MVC模式建议将代码分割成3个部分。在创建应用程序的类/文件时,开发者必须将其归入以下三个层次之一。

  • 模型:**该组件存储应用程序的数据。它对接口没有了解。模型负责处理领域逻辑(现实世界的业务规则)以及与数据库和网络层的通信。
  • 视图:**它是UI(用户界面)层,持有屏幕上可见的组件。此外,它提供了存储在模型中的数据的可视化,并为用户提供交互。
  • 控制器:**这个组件建立了视图和模型之间的关系。它包含了核心的应用逻辑,了解用户的反应并根据需要更新模型。

The Model-View-Controller(MVC) Pattern

模型-视图-展示者(MVP)模式

MVP模式克服了MVC的挑战,提供了一种简单的方法来构造项目代码。MVP被广泛接受的原因是它提供了模块化、可测试性,以及一个更干净和可维护的代码库。它由以下三个部分组成。

  • 模型:用于存储数据的层。它负责处理领域逻辑(现实世界的业务规则)以及与数据库和网络层的通信。
  • 视图: UI(用户界面)层。它提供数据的可视化,并跟踪用户的动作,以便通知Presenter。
  • Presenter:从模型中获取数据并应用UI逻辑来决定显示什么。它管理视图的状态,并根据用户从视图的输入通知采取相应的行动。

The Model-View-Presenter(MVP) Pattern

模型-视图-视图模型(MVVM)模式

MVVM模式与MVP(Model-View-Presenter)设计模式有一些相似之处,因为Presenter的角色是由ViewModel扮演的。然而,MVVM已经解决了MVP模式的缺点。它建议将数据表现逻辑(视图或用户界面)与应用程序的核心业务逻辑部分分开。MVVM的独立代码层是。

  • 模型:**该层负责数据源的抽象化。模型和ViewModel一起工作来获取和保存数据。
  • View: 本层的目的是将用户的操作告知ViewModel。这一层观察ViewModel,不包含任何形式的应用逻辑。
  • ViewModel:它暴露了那些与视图有关的数据流。此外,它作为模型和视图之间的链接。

The Model - View - ViewModel (MVVM) Pattern

MVC、MVP和MVVM设计模式之间的区别

MVC(model view controller) MVP(model view presenter) MVVM(model view viewmodel)
最古老的软件架构之一 作为软件架构的第二次迭代而开发,是MVC的进步。 业界公认的应用程序的架构模式。
UI(View)和数据访问机制(Model)是紧密耦合的。 它通过使用Presenter作为ModelView之间的通信渠道,解决了依赖**View的问题。 这种架构模式是更多的事件驱动的,因为它使用数据绑定,因此很容易将核心业务逻辑与**视图分离。
控制器视图以一对多的关系存在。一个控制器可以根据需要的操作选择不同的视图。 PresenterView之间存在一对一的关系,因为一个Presenter类同时管理一个View。 多个View可以被映射到一个ViewModel上,因此,View和ViewModel之间存在一对多的关系。
ViewController不了解。 ViewPresenter有引用。 ViewViewModel有引用。
由于代码层是紧密耦合的,因此很难对应用程序的功能进行更改和修改。 代码层是松散耦合的,因此很容易对应用代码进行修改/变更。 容易在应用程序中进行修改。但是,如果数据绑定逻辑太复杂,调试应用程序就会有点困难。
用户输入由**控制器处理。 视图是应用程序的入口 视图接受用户的输入,并作为应用程序的入口。
只适合于小规模的项目。 适用于简单和复杂的应用。 不适合小规模的项目。
对**单元测试的支持有限。 容易进行单元测试,但View和Presenter的紧密结合会使其略显困难。 在这个架构中,单元测试性是最高的。
该架构对Android APIs的依赖性较高。 该架构对Android APIs的依赖性较低。 对Android APIs的依赖性低或没有依赖性。
它不遵循模块化和单一责任原则。 遵循模块化和单一责任原则。 遵循模块化和单一责任原则。

MVP和MVC的关键区别在于,MVP中的展示者在模型和视图之间的交流中扮演着更积极的角色,并负责控制两者之间的数据流。MVVM是Model-View-ViewModel的缩写。在MVVM模式中,"Model "代表应用程序的数据和逻辑,"View "代表应用程序的用户界面,而 "ViewModel "是位于Model和View之间的一层,负责将Model的数据和逻辑以一种更容易操作的方式暴露给View。ViewModel也处理用户的输入,并根据需要更新Model。

总的来说,这些模式的主要区别在于中介组件的作用。MVC和MVP都涉及到一个控制器或展示者,作为模型和视图之间的中介,而MVVM涉及到一个ViewModel,作为模型和视图之间的中介。MVC是这些模式中最简单的,而MVP和MVVM则更灵活,允许在应用程序的不同层之间进行更清晰的分离。


来自GeeksForGeeks

标签:MVP,架构,MVVM,ViewModel,视图,Model,View
From: https://www.cnblogs.com/zhuangzhongxu/p/17173244.html

相关文章