I try to understand the main differences between MVC / MVP and MVVM patterns. I found these 3 diagrams but I'm not sure to understand them. Coul you help me and explain me what is the legend of the dashed line and continuous line.
问题:
回答1:
- Solid lines are direct calls.
- Dashed lines are only event callbacks.
Main differences between MVC and MVP (Passive view) patterns:
- In MVC view knows about model (calls getData() etc. to display data)
- In MVP (Passive view) the view does not know about model. Presenter passes data from model to view.
More details in:
- MVC vs MVP vs MVVM
- In depth description by Martin Fowler: GUI Architectures
回答2:
I think the dotted lines are indirect references
I'm not as familiar with MVC or MVP, but in MVVM a View
references a ViewModel
, and the ViewModel
references the Model
, which is represented by solid lines.
The Models
can broadcast messages or raise event notifications which are picked up by the ViewModel
, and ViewModels
can publish events that are picked up by the View
, however these objects should never directly reference the other object, so they're indirect references. For example, a programmer is aware that the purpose for raising an event notification on a Model
is so that the ViewModel
can hook into the event and process something, however the Model
itself never references the ViewModel
.
It should be noted if you're comparing the patterns, that they are very different patterns that just happen to share the same naming convention for some objects. For example, a Model
in MVC is not the same as a Model
in MVVM. Instead, MVC's M+C
is equal to MVVM's VM
, and MVC's M
contains a mix of both MVVM's M
and VM
pieces
回答3:
The dotted lines are notifications (e.g. observer pattern) and the solid lines are direct knowledge (i.e. compile time dependencies). Data change notifications are flowing on the dotted lines. A solid line with an arrow says that one component has knowledge of the other and can directly push data. A dotted line is looser coupling as the sender is firing out an event but doesn't know the nature of the receiver of that event which is hidden behind an event listener interface (if you are doing event driven versions of those patterns).
The point of the patterns is to create order by avoiding spaghetti code where everything directly interacts with everything else. So the diagrams are really only hints about what should be decoupled from what. Like any such diagrams they are hard to grok without a detailed explanation and they are only really indicative of what you should aim for; certain frameworks have more or less support for doing things in a "pure way". How components get loaded and wired together is not in the scope of those diagrams; only what happens when the user inputs data or the model is updated through a different view component. So the actual classes may have compile time dependencies and code to initialise the objects which seem to violate the diagrams; yet so long as it is just "initialisation" code which is connecting things together it may not be material.
Here is a presentation which tries to explain MVP, MVC (or possibly MVVMP) and MVVM (aka MVB) in terms of some less formal diagrams which show what compiles to what and who notifies whom with observer pattern event listeners. It is relevant to your question as it sets the context about what the patterns aim to achieve which helps in interpreting the diagrams in your question:
Design Patterns in ZK: Java MVVM as Model-View-Binder, Simon Massey
Here is an article which doesn't have the diagrams in it but which does the same simple screen three times using three different GUI event driven desktop patterns (which can be loosely described as MVP, MVVM and MVC/MVVMP). One key point of confusion about the M__ patterns is that they are overloaded monikers and hardly very descriptive or indicative of the actual pattern. The article is relevant to your question as it follows Martin Fowlers formal description of the patterns which are clearer and less confusing than their "M__" names:
Implementing event-driven GUI patterns using the ZK Java AJAX framework, Simon Massey
Whilst that article does not specifically answer your question it does give a comparison of three implementations of the patterns you are asking about and compares them; so it is likely to shed some light on what the choices the patterns are making which the diagrams are meant to describe. Of course if you pick a different framework to implement the three patterns the example code would look different; but hopefully the same trade-offs would be seen as with the examples shown in that article.
回答4:
MVC is used in java architectures such as Spring, Struts etc.. MVC stands for Model view and container.
it is very good to use this strategy in Web application
Model–view–controller (MVC) is a software architecture,[1] currently considered an architectural pattern, used in software engineering. The pattern isolates domain logic (the application logic for the user) from the user interface (input and presentation), permitting independent development, testing and maintenance of each (separation of concerns).
Use of the MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.[2]