MVP MVVM MVC

Source: Internet
Author: User

I am very grateful for the attention we have received from the previous article. Because of their own understanding of these models is also limited, for MVC,MVP,MVVM These models of comparison, is a combination of their own understanding, some places may not be accurate, the need for friends can refer to the next one to get everyone's attention, thank you very much. Because of their own understanding of these models is also limited, for these models of comparison, is a combination of their own understanding, some places may not be accurate, but only to highlight their own views, can not be a good idea? Welcome you to shoot bricks.  :) Copy the code code as follows: Reading table of Contents: four. MVP Mode 4.1 MVP idea 4.2 UI interface 4.3 presenter--model and view Bridge 4.4 MVP code structure and timing diagram 4.5 MVP Model Summary Five. The design idea of the MVVM pattern 5.1 MVVM Pattern 5.2 The MVVM pattern structure figure six. MVC, MVP and MVVM pattern Usage Summary Four, MVP mode MVP mode is also a classic interface mode. The MVP's M represents model, V is view, and P is presenter. The complete code in the following example can be downloaded here: WINFORMMVP source You can also compare this article analysis on the v-p interaction problem and case sharing in MVP Mode 4.1 MVP model in my opinion, is a real sense of isolation view of the details and complexity of the pattern. Why do you say this: because in other modes v represents the UI interface, it is an HTML page, a XAML file, or a WinForm interface. But in MVP mode, V represents an interface, an interface that abstracts the UI interface. interface means that any interface that implements the interface can reuse existing presenter and model code. 4.2 UI interface to understand the MVP well, there is the ability to interface the UI interface.       Look at the following interface, the red flag of the user control abstraction, you can get the following interface copy code code as follows: public interface Iuseradd {event EventHandler useraddevent;       String UserName {get; set;} String Userage {get; set;}} The 2 input boxes in the interface are abstracted into two properties of username and Userage. The Click event of the Save button is abstracted into the event useraddevent. The code that implements the interface in WinForm is as follows: Copy code as follows: public partial class Useradd:usercontrol, Iuseradd {public event EventHandler useraddevent;            public string UserName {set {this.txbName.Text = value;}        get {return this.txbName.Text;}            } public string Userage {set {this.txbAge.Text = value;}        get {return this.txbAge.Text;}        } public UserAdd () {InitializeComponent (); } private void btnAdd_Click (object sender, EventArgs e) {if (useraddevent! = null) useraddevent (th        is, e); }} take the Userage property below to explain the magic of UI interface. When the backend code to get the age value on the interface, just need get property, to update the interface display, just need set property. At this time, the back-end code for the interface operation, is abstracted to the userage properties of the operation, that is, and the specific interface display is irrelevant. The bridge between 4.3 Presenter--model and view contains both p and M in the back-end code mentioned above. M, like MVC, refers to logical code. P is the bridge between the model and view, which is responsible for combining the corresponding model with the view.        For the above Iuseradd, the corresponding presenter code is: Copy code code is as follows: public class Useraddpresenter:ipresenter {private readonly iuser _model; Private ReadOnly Iuseradd _view Private readonly Applicationfacade _facade = applicationfacade.instance; Here the facade is used for communication between presenter, in detail can be seen in the full code//presenter constructor, the view and model as parameters to the public useraddpresenter (Iuser model, I            UserAdd view) {_model = model;            _view = view;        Wireupviewevents (); } private void Wireupviewevents () {_view.        Useraddevent + = _view_useradd;     }//When the Useradd event of the view is triggered, the data in the UI is obtained, the model logic processing is invoked, and the new user is added. The user_added message is sent to the system (the other UI part of the system receives the message, such as the DataGrid here, which refreshes after it receives user_added) private void _view_useradd (object sender, EventArgs e) {var user = new User {Name = _view. UserName, age = Convert.ToInt32 (_view.           Userage)}; _model.            AddItem (user); _facade.        SendNotification (applicationfacade.user_added); }}4.4 MVP code structure and timing diagram Here the MVP code structure and timing diagram, can better help understand the MVP Model 4.5 MVP Model Summary in the MVP, presenter finishThe model and view are separated, and the main program logic is implemented in presenter. Furthermore, presenter is not directly related to the specific view, but interacts through a well-defined interface, allowing the presenter to be kept constant when the view is changed, that is, reuse! Not only that, we can also write Test view, simulate the user's various operations, so as to achieve the test of presenter-without the need to use automated testing tools. We can even test the logic of presenter by writing a mock Object (that is, an interface that implements the model and view, but no concrete content) when the model and view are not finished. MVP Advantage 1, the model and the view completely separate, we can modify the view without affecting the model 2, can more efficient use of the model, because all the interaction occurs in a place--presenter internal 3, we can use a presener for multiple views, Without the need to change the logic of presenter. This feature is very useful because view changes are always more frequent than models. 4, if we put logic in presenter, then we can test these logic out of the user interface (unit test) Five, the MVVM pattern 5.1 The design of the MVVM pattern in the MVVM pattern, a viewmodel and a view match, It does not have the IView interface in the MVP, but the full and view bindings, all changes in the view are automatically updated to ViewModel, and any changes to the ViewModel are automatically synchronized to the view display. The reason this automatic synchronization is possible is that the properties in ViewModel implement an interface such as observable, which means that when a property's set method is used, the property modification event is triggered at the same time, so that the bound UI is automatically refreshed. (in WPF, this observable interface is inotifypropertychanged; in Knockoutjs, it is implemented by the function ko.observable () and ko.observrablecollection ()) So MVVM is a step ahead of the MVP, in the MVP, V is the interface iview, which solves the interface UI coupling; While MVVM simply integrates seamlessly with ViewModel and UI, ViewModel directly represents the UI. But MVVM does this by relying on specific platforms and technologies, such as WPF and Knockoutjs, which is why ViewModel does not need to implement interfaces, because reliance on specific platforms and technologies is inherentlyUsing the MVVM pattern is a platform that cannot replace the UI. 5.2 MVVM Schema diagram here is the structure diagram of the MVVM pattern, which can help to understand the MVVM pattern more easily: Six, MVC, MVP, and MVVM patterns using scenario summary because in WinForm, you can't like WPF, Supports two-way binding of data and interfaces and monitoring of events, so MVP is the best choice in WinForm. Using knockout in WPF and HTML interfaces, observable is implemented, so use MVVM. (It should be said that WPF is designed for use with MVVM) in Web applications, because HTTP works in a request-and-response manner and cannot remain connected. So MVC is the best choice when it's impossible to get the message passing between presenter in the MVP and the ViewModel and interface in MVVM. You may be interested in the article: ASP. NET summary of MVC, MVP, MVVM comparison and difference (i) the difference analysis between virtual and abstract in ASP. Response.Charset and Response.ContentEncoding in ASP. C #. The difference between const and readonly in net in detail explains the difference between DataReader and Datasets in ASP. Dbnull.value,null,string.empty difference in ASP. The difference between the static method and the dynamic method call analysis. NET difference between as and is analysis of the difference between the control and the motherboard of ASP. NET, the difference between the session and the cookie and how to use the. NET MVC in ViewData, Analysis on the difference between ViewBag and TempData

  

MVP MVVM MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.