26

我正在使用 Flutter 创建一个新应用程序,并且我正在尝试设计它,将业务逻辑与视图分离。

我读过 Bloc 和 MVVM(我知道还有其他模式,但这些是我更喜欢的模式),但我不明白它们之间的区别。在我看来,它们几乎相同。

有谁可以帮我理解它们?

4

3 回答 3

18

查看MVVM的此插图(来源):

您可以看到有单独的数据和业务逻辑模型。但是,使用BLoC并没有真正的区别。处理业务逻辑的类也处理数据,这也适用于MVVM

说句公道话,其实差别不大。两者的关键部分是相同的:将业务逻辑与 UI 隔离。因此,两者中任何一个的实现看起来都非常相似,即使用Stream's 和StreamBuilder's。
此外,还有一些包可以让使用Stream' 变得更容易,例如rxdart,就我而言,这是 Flutter 团队使用的。

于 2019-03-01T10:53:23.847 回答
11

它们并不完全相同,实际上...... MVVM 意味着视图和视图模型之间的数据绑定,这意味着,实际上,视图对象主要是命令视图模型的对象。在我看来,MVVM 是 MVC 的简化,在幕后“按原样”展示模型。例如,Xamarin 主要使用 MVVM 和屏幕上的控件,如复选框、文本输入等,都会在后台修改模型视图。

您可能已经开始在这里看到一个问题:如果您更改 UI,您可能还必须更改 MV。假设你有一个条目号必须在 0-255 之间,你把这个逻辑放在哪里?好吧,在 MVVM 上,你把这个逻辑放在视图上。但是您也必须将这些锁放在模型视图上以保证数据安全。这意味着要重写很多代码来做同样的事情。如果您决定更改此范围,则必须更改两个位置,这会使您的代码更容易出错。免责声明:有解决方法,但远比它应该的复杂。

On the other hand, BLoC works by receiving events and emitting states. It doesn't care (although it may) from where the event came from. Using the same example from the above, the view would signal an event to the bloc/controller with "hey, my number changed!", the bloc then would process this new event and, if suitable, emit a signal to the UI: "hey UI! You should change! I have a new state for you!". Then, the UI rebuilds itself to present those changes.

For me, the advantage of BLoC over MVVM is that the business logic can be entirely decouple from the view, which is overall a better way to do things. As our modern software development requires more and more changes in the UI (being different screen sizes, densities, platform, etc.), having the UI side decoupled from the models are a fantastic feature to code reusability.

于 2020-08-26T14:07:37.417 回答
8

BLoC and MVVM seemed to be different when BLoC was introduced, but that differences faded away as BLoC implementations changed over time. Right now the only real difference is that BLoC doesn't specify a separate presentation logic and business logic, or at least it doesn't do it in an obvious manner. Presentation logic is the layer that understands interactions between UI elements and the business part of the application(Presenter job in MVP). Some BLoC implementations put presentation logic into BLoC's, some others into UI.

The NEW THING in BloC was that it should not expose any methods. Instead, it would only accept events through its exposed sink or sinks. This was for sake of code reuse between Angular Dart web apps and Flutter mobile apps. This concept was recently abandoned because we don't really write Angular Dart web apps and it is less convenient than regular methods. Right now Blocks in official BLoC package expose methods just like good ol' VM.

Some would say that BLoC should expose one Stream of complete state objects, while VM can expose multiple Streams, but this is not true. Exposing one Stream of states is a good practice in both approaches. At first, official Google BLoC presentations presented BLoCs implemented using multiple output Streams as well.

One interesting difference that seemed to be a thing was that BLoC should communicate via events not only with UI but also with different parts of the application. for example, it should receive an event after receiving Firebase notification or when Repository data changes. While this seems interesting I've never seen an implementation like that. It would be odd from a technical point of view (Repository would have to know about all BLoC's that are using it???). Although I am thinking about trying out such an implementation that would be based on EventBus but that's completely off topic :)

于 2020-11-03T10:00:13.680 回答