简体   繁体   English

Android 中具有干净架构的 mvvm 和没有干净架构的 mvvm 有什么区别?

[英]What is difference between mvvm with clean architecture and mvvm without clean architecture in Android?

I'm learning about MVVM and Clean Architecture.我正在学习 MVVM 和 Clean Architecture。 Then I found some articles present about MVVM + Clean Architecture, but I still didn't get it about the difference between mvvm with clean architecture and mvvm without clean architecture.然后我发现了一些关于 MVVM + Clean Architecture 的文章,但我仍然没有理解 mvvm with clean architecture 和 mvvm without clean architecture 之间的区别。 Any summary about these stuff?关于这些东西有什么总结吗? Thank you.谢谢。

Clean architecture aims to separate the layers.清洁架构旨在分离各层。 Business Layer, Data Layer and Presentation Layer will be separate applications.业务层、数据层和表示层将是独立的应用程序。 Therefore you will increase the reusability for each one of them.因此,您将增加它们中的每一个的可重用性。 MVVM as design pattern should be implemented in the Presentation Layer. MVVM 作为设计模式应该在表示层中实现。 Presentation Layer will interact with Business Layer (or Domain Layer) and the Business Layer will use Data Layer for sharing data.表示层将与业务层(或领域层)交互,业务层将使用数据层来共享数据。

MVVM is just part of the clean architecture in the presentation layer . MVVM 只是presentation layer中干净架构的一部分。 It just a set of rules on how to display the data from UseCase.它只是一组关于如何显示来自 UseCase 的数据的规则。

One of the benefits of using clean architecture is we can change our design pattern in presentation layer without changing domain layer or use case.使用干净架构的好处之一是我们可以在presentation layer更改我们的设计模式,而无需更改domain layer或用例。

So for example, if we're using let say MVI and then changing to MVVM, it can be done smoothly with ease.. :)因此,例如,如果我们使用 let say MVI,然后更改为 MVVM,则可以轻松顺利地完成.. :)

MVVM is just a technique to manage the View layer of whatever architecture you're using. MVVM只是一种管理您正在使用的任何架构的视图层的技术。

Clean Architecture is a way of organizing communication between layers.清洁架构是一种组织层之间通信的方式。 They aren't mutually exclusive它们不是相互排斥的

The Layers of MVVM with Clean Architecture The code is divided into three separate layers: The Layers of MVVM with Clean Architecture 代码分为三个独立的层:

  • Presentation Layer表示层
  • Domain Layer领域层
  • Data Layer数据层

Presentation Layer表示层
Is here, where the logic related with views and animations happens.在这里,与视图和动画相关的逻辑发生在这里。 It uses Model-View-ViewModel ( MVVM ), but you can use any other pattern like MVC or MVP它使用Model-View-ViewModel ( MVVM ),但您可以使用任何其他模式,如MVCMVP

Main differences:主要区别:

  1. MVVM (https://learn.microsoft.com/en-us/do.net/architecture/maui/mvvm ) is built around a "model" (business rules), where a database is a part of the "model". MVVM (https://learn.microsoft.com/en-us/do.net/architecture/maui/mvvm ) 是围绕“模型”(业务规则)构建的,其中数据库是“模型”的一部分。 在此处输入图像描述
  2. The same goes for Android's "whatever name is" architecture (data<-domain<-UI) https://developer.android.com/topic/architecture . Android 的“无论名称是什么”架构也是如此 (data<-domain<-UI) https://developer.android.com/topic/architecture It is built around the "data" layer, making it very difficult to switch between databases, but allows easily manipulate with UI.它围绕“数据”层构建,因此很难在数据库之间切换,但可以轻松地使用 UI 进行操作。 在此处输入图像描述
  3. The clean architecture ( https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html ) is built around "business rules", where everything is very flexible and the database and UI are both can be replaced at any time.干净的架构( https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html ) 是围绕“业务规则”构建的,其中一切都非常灵活,数据库和 UI两者都可以随时更换。 在此处输入图像描述

Here is a quote from the book "Clean Architecture: A Craftsman's Guide to Software Structure and Design":以下是《清洁架构:软件结构和设计工匠指南》一书中的一段话:

The architect can employ the Single Responsibility Principle and the Common Closure Principle to separate those things that change for different reasons, and to collect those things that change for the same reasons—given the context of the intent of the system.架构师可以使用单一职责原则和共同封闭原则来分离那些因不同原因而改变的事物,并收集那些因相同原因而改变的事物——给定系统意图的上下文。 User interfaces change for reasons that have nothing to do with business rules.用户界面因与业务规则无关的原因而改变。 Business rules themselves may be closely tied to the application, or they may be more general.业务规则本身可能与应用程序紧密相关,或者它们可能更通用。 The database, the query language, and even the schema are technical details that have nothing to do with the business rules or the UI.数据库、查询语言甚至模式都是与业务规则或 UI 无关的技术细节。

Thus we find the system divided into four decoupled horizontal layers — the application-independent business rules, Application-specific business rules, UI, and the Database.因此,我们发现系统分为四个解耦的水平层——独立于应用程序的业务规则、特定于应用程序的业务规则、UI 和数据库。

Later in the book Robert Martin describes in detail how to build these 4 layers: entities <- use cases <- interface adapters <- frameworks and drivers. Robert Martin 在本书的后面详细描述了如何构建这 4 层:实体 <- 用例 <- 接口适配器 <- 框架和驱动程序。

Here is another quote:这是另一个引用:

If the application wants to display money on the screen, it might pass a Currency object to the Presenter.如果应用程序想要在屏幕上显示货币,它可能会将货币 object 传递给 Presenter。 The Presenter will format that object with the appropriate decimal places and currency markers, creating a string that it can place in the View Model. If that currency value should be turned red if it is negative, then a simple boolean flag in the View model will be set appropriately. Presenter 将使用适当的小数位和货币标记格式化 object,创建一个字符串,它可以放置在视图 Model 中。如果该货币值是负数,则应将其变为红色,然后视图 model 中的一个简单的 boolean 标志将适当设置。 Anything and everything that appears on the screen, and that the application has some kind of control over, is represented in the View Model as a string, a boolean, or an enum.屏幕上出现的任何事物,以及应用程序对其具有某种控制权的事物,在视图 Model 中都表示为字符串、boolean 或枚举。 Nothing is left for the View to do other than to load the data from the View Model into the screen.除了将视图 Model 中的数据加载到屏幕之外,View 没有什么可做的了。

As you can see ViewModel from the clean architecture is more like a "state" of the view, and the logic of the View goes to the " Presenter ".正如您从干净的架构中看到的那样, ViewModel更像是视图的“状态”,View 的逻辑转到“ Presenter ”。 In MVVM on another hand, ViewModel holds both state and logic of the View.另一方面,在 MVVM 中,ViewModel 包含 state 和 View 的逻辑。

In summary: MVVM and Clean Architecture are completely different and there is no way you can build a MVVM with clean architecture.总结: MVVM 和 Clean Architecture 是完全不同的,您无法构建具有干净架构的 MVVM。

As I understand,我认为,

MVVM without Clean Architecture没有干净架构的 MVVM

______________________________________________

UI
- - - - - - - - - - - - - - - - - - - - - - - 

Presenter/ViewModel        (Bussiness Logic)
______________________________________________

Repository

DataSource
______________________________________________

MVVM with Clean Architecture具有干净架构的 MVVM

______________________________________________

UI
                                                Presentation Layer
Presenter/ViewModel        
______________________________________________

UseCases + Entity          (Bussiness Logic)    Domain/Bussiness Layer
______________________________________________

Repository
                                                Data Layer
DataSource
______________________________________________

Clean Architecture is a set of rules and principles that helps you maintain and architect a software system. Clean Architecture 是一组规则和原则,可帮助您维护和构建软件系统。 While MVVM is a Design Pattern (similarly like other ones MVP, MVC) that is originated from Clean Architecture.而 MVVM 是一种源自 Clean Architecture 的设计模式(类似于其他的 MVP、MVC)。

As Clean Architecture strongly recommends to separate each layer UI, Controllers, Use-cases & Entities, MVVM does the same job as it separates each of them in:正如 Clean Architecture 强烈建议将每一层 UI、控制器、用例和实体分开一样,MVVM 的工作与将它们分开时的工作相同:

  • UI -> View用户界面 ->视图
  • Controllers -> ViewModel控制器 ->视图模型
  • Use-cases/Entities/APIs/Local/Repos/etc.用例/实体/API/本地/存储库/等。 -> Model -> Model

That's what I understood.我就是这么理解的。 Hope this helps:)希望这可以帮助:)

Clean Architecture with MVVM使用 MVVM 清理架构

Three Layers三层

  1. Presentation (Model-View-ViewModel)演示文稿(模型-视图-视图模型)
  2. Domain (or Business) Layer - [Use Case, Entity, Repository (for testing)]域(或业务)层 - [用例、实体、存储库(用于测试)]
  3. Data Layer - [Repository (Impl), Network (Retrofit), Local (Room)]数据层 - [Repository (Impl), Network (Retrofit), Local (Room)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有实时数据的 Android MVVM 清洁架构 - Android MVVM clean architecture with livedata 如何在 android 中为 MVVM Clean 架构创建用例 - How to create UseCase for MVVM Clean architecture in android 如何在没有 DI 并遵循干净架构的情况下在 MVVM 的存储库中传递上下文? - How to pass context in Repository in MVVM without DI and following the clean architecture? 功能与 MVVM 相似的 2 个片段的干净架构? - Clean architecture for 2 fragments that are similar in function with MVVM? 在 Clean architecture MVVM 中,将对象转换为适配器? - In Clean architecture MVVM where transform objects to adapter? 如何在 MVVM 中为 Repository 提供 Drawable,干净的架构 - How to Providing Drawable for Repository in MVVM, Clean Architecture MVVM 清洁架构中是否真的需要 Data Mapper 层? - Does the Data Mapper layer is really required in MVVM clean architecture? 用例中的道。 MVVM 或 Clean Architecture 反模式? - Dao in Usecase. MVVM or Clean Architecture anti-pattern? 在 MVVM 架构中检查 SELECT 查询结果长度的干净方法是什么? - What is the clean way to check a SELECT query result's length in MVVM architecture? 带有Dagger 2的Android Clean MVVM - Android Clean MVVM With Dagger 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM