问题
If we search Google using the phrase "What is difference between MVC, MVP & MVVM design pattern" then we may get few URL's which discuss the difference between MVC MVP & MVVM design pattern theoretically like:
MVP
Use in situations where binding via a datacontext is not possible. Windows Forms is a perfect example of this. In order to separate the view from the model, a presenter is needed. Since the view cannot directly bind to the presenter, information must be passed to the view via an interface (IView).
MVVM
Use in situations where binding via a datacontext is possible. Why? The various IView interfaces for each view are removed which means less code to maintain. Some examples where MVVM is possible include WPF and javascript projects using Knockout.
MVC
Use in situations where the connection between the view and the rest of the program is not always available (and you can’t effectively employ MVVM or MVP). This clearly describes the situation where a web API is separated from the data sent to the client browsers. Microsoft’s ASP.NET MVC is a great tool for managing such situations and provides a very clear MVC framework
But I have not found a single article which discuss the difference theoretically along with sample code.
It would be really nice if i get a article which discuss the difference between these 3 design patterns (MVC, MVP & MVVM) along with code.
I'd like to get my hands on source code of 3 similar CRUD apps which has been implemented by these three design patterns (MVC, MVP & MVVM). So that I can go through the code and understand how one should write code for these three design patter (MVC, MVP & MVVM).
So if any such article exists which discuss how code would look different for these 3 design patterns (MVC, MVP & MVVM) then please redirect me to that article.
回答1:
This should be a good starter. In reality, your chosen 'platform' also plays a large role in how you use these patterns. For example, MVVM is naturally suited to WPF, while MVP works well with Windows Forms. ASP.Net MVC speaks for itself.
回答2:
Some basic differences can be written in short:
MVC:
Traditional MVC is where there is a
- Model: Acts as the model for data
- View : Deals with the view to the user which can be the UI
- Controller: Controls the interaction between Model and View, where view calls the controller to update model. View can call multiple controllers if needed.
MVP:
Similar to traditional MVC but Controller is replaced by Presenter. But the Presenter, unlike Controller is responsible for changing the view as well. The view usually does not call the presenter.
MVVM
The difference here is the presence of View Model. It is kind of an implementation of Observer Design Pattern, where changes in the model are represented in the view as well, by the VM. Eg: If a slider is changed, not only the model is updated but the data which may be a text, that is displayed in the view is updated as well. So there is a two-way data binding.
回答3:
MVC, MVP, MVVM
MVC (old one)
MVP (more modular because of its low-coupling. Presenter is a mediator between View and Model)
MVVM (You already have two-way binding between VM and UI component, so it is more automated than MVP)
Another image:
回答4:
Great Explanation from the link : http://geekswithblogs.net/dlussier/archive/2009/11/21/136454.aspx
Let's First look at MVC
The input is directed at the Controller first, not the view. That input might be coming from a user interacting with a page, but it could also be from simply entering a specific url into a browser. In either case, its a Controller that is interfaced with to kick off some functionality.
There is a many-to-one relationship between the Controller and the View. That’s because a single controller may select different views to be rendered based on the operation being executed.
There is one way arrow from Controller to View. This is because the View doesn’t have any knowledge of or reference to the controller.
The Controller does pass back the Model, so there is knowledge between the View and the expected Model being passed into it, but not the Controller serving it up.
MVP – Model View Presenter
Now let’s look at the MVP pattern. It looks very similar to MVC, except for some key distinctions:
The input begins with the View, not the Presenter.
There is a one-to-one mapping between the View and the associated Presenter.
The View holds a reference to the Presenter. The Presenter is also reacting to events being triggered from the View, so its aware of the View its associated with.
The Presenter updates the View based on the requested actions it performs on the Model, but the View is not Model aware.
MVVM – Model View View Model
So with the MVC and MVP patterns in front of us, let’s look at the MVVM pattern and see what differences it holds:
The input begins with the View, not the View Model.
While the View holds a reference to the View Model, the View Model has no information about the View. This is why its possible to have a one-to-many mapping between various Views and one View Model…even across technologies. For example, a WPF View and a Silverlight View could share the same View Model.
回答5:
The image below is from the article written by Erwin van der Valk:
The article explains the differences and gives some code examples in C#
来源:https://stackoverflow.com/questions/19444431/what-is-difference-between-mvc-mvp-mvvm-design-pattern-in-terms-of-coding-c-s