A summary of the development and application of MVC

Source: Internet
Author: User

1, the original program is the command line interface, and later evolved to the GUI (Graphic user Interface), that is, the graphical interface. Take a calculator software for example, a program whether C/s or b/s structure need to be done:

A) interface layout. The entire calculator interface how to layout look reasonable, beautiful.

b) Business logic. For example, when the user taps the "=" button, the output is calculated based on the input.

c) interface interaction. For example, when the user clicks the "=" button, the interface responds, such as displaying the results and displaying the results.

No MVC situation, such as WinForm in a form, that is, there is a calculator interface display code, but also trigger the listener after the process control logic, as well as the functional logic calculation code, the three mixed together, the program is complex, the code length is amazing, modify, expand, read others code is a problem.

2, the desktop GUI program MVC design mode.

Conforms to the "low coupling, cohesion poly" software engineering concept. Where's the show? In MVC, the interface display code is effectively isolated from the business logic code, separating the original melted from the code, the parts tending to specialize, and the functional responsibilities being single. This makes the program easier to read, maintain, and expand.

  

A) View: Responsible for the interface display.

b) Controller: Responsible for the interaction between view, Model, service, and control the business process. No specific business function code is involved. The desktop-side controller can be an anonymous implementation class for component listeners.

c) Model: a simple data model that encapsulates the data in the view so that it can be passed and used in other layers (in the form of objects). is a data model, not a business model, and does not involve specific business function code.

d) Service: Encapsulates business logic that specifically implements business functions, such as program function implementation, database operations, third-party library calls, and so on. The service is highlighted here to emphasize that model is only a data model, controller is process control, they do not contain business function code, the business function code is concentrated in the service layer.

The implementation of desktop MVC. MVC focuses on the view layer, which is implemented in the service layer, then encapsulates the model for data sharing, and the controller layer to reconcile the interaction and flow control of each layer. In this way, the separation of the display from the business is realized.

  

1) View and model application observer design mode, on the one hand, view from the model to get the data rendered to the user, on the other hand, when the model is modified to automatically refresh into the view and display it.

2) View and controller can apply the strategy mode, increase the flexibility of the program.

3) Controller combination service (interface oriented) and model.

Example of a Java swing using MVC:

Public abstract class view{

public void update ();

}

public class Viewimpl extends view{

The interface element properties of the view.

Model model;//Application Viewer mode

Controller controller;//combination, can be used in policy mode

Public Viewimpl () {

Model = new Modelimpl (this);

Controller = new Controller (model)

Interface Layout method invocation, initializing component method calls (using model data)

}

Xxx.addactionlistener (New actionlistener{

@Override

public void Actionperform () {

Refresh the interface component data into model

Controller.do1 ();

}

})

@Override

public void Update () {

Update the data in the model to the view's interface component.

}

}

public interface controller{

public void Do1 ();

public void Do2 ();

public void Do3 ();

}

public class Controllerimpl implements controler{

Model model;

Public controler (Model m) {

model = m;

}

@Override

public void Do1 () {

Getting data from a model object

Execute business logic and processes

Results updated to model

Some other feature calls and process controls.

Model.notifyview ();

}

}

public interface model{

public void Notifyview ();

}

public class modelimpl{

Model Properties, etc.

View view;

Public Modelimp (View v) {

view = V;

}

public void Notifyview () {

View.update ();

}

}

public class application{

public static void Main (string[] args) {

After user login and verification pass

View Viewimpl = new Viewimpl ();

View.visable=true;

}

}

Directory structure: This way, 1) conforms to interface-oriented programming (Controller and model) and abstract programming (View), separating design from implementation. 2 Each form is composed of view, Controller, model three files, the function is clear, the responsibility is single, easy to develop and maintain.

|-form

|-view.java

|-controller.java

|-model.java

|-viewimpl_1

|-viewimpl.java

|-controllerimpl.java

|-modelimpl.java

|-viewimpl_2

|-viewimpl_3

|-service

|-dao

|-dbutilies.java

|-userdao.java

|-impl

|-userdaoimpl.java

|-entity

|-user.java

Think: MVC is just a design idea, and it's a diverse way of achieving it.

1) In this way, each page from the original mix together to divide into three pages, so that the responsibility of a single, easy to develop and maintain.   But obviously there are many extra files out, when the size of the program is not to be divided into three? 2) The above view and Controller,controller and model between them is a combination of relationships, whether the policy mode can be used to allow view to use different controllers according to different situations?

3) above through the view of the role of the listener only call controller, the real process control, function calls implemented in Controler, can directly use the listener's anonymous implementation class as controller, and reduce the additional controller files? So what about the implementation classes that consider using controller classes as listeners?

4) How to create the object, whether to consider the factory design mode, further decoupling? There is also the problem of visibility, controller only visible model and service, that want to control the state of view, do you need to encapsulate other tool classes? How are these issues solved with the spring IOC? And what about the DAO module with MyBatis?

5) controller in both processing function calls and processing page interaction, generally can single-threaded, first control function calls, after the call to the results of the model, the final control of the view display interface and model data. Can multi-threading, can the function call and page interaction asynchronous processing? Does that need to be synchronized?

3. The MVC pattern of web-side programs.

Then the B/s structure of the program began to slowly develop from static Web pages to dynamic Web pages. Let's say that you do not use MVC design patterns, such as Java EE, only with JSP development. Then a JSP page has HTML, CSS, JS composed of visual interface display, as well as Process Control code, and access to the database and other Java programs. In order to solve the complexity of development, we also use MVC design pattern in B/S.

  

(typical Jsp+servlet+javabean implementation of MVC)

The MVC pattern of the web is the same as the desktop-side MVC principle, but there is a big difference in implementation, especially the model layer. The model layers of both are functionally identical, and are simply encapsulated objects.

Implementation, previously said that the desktop view and model is the Observer mode, view from the model to get the data rendered to the user, and when the model is modified to automatically refresh into the view and display it. In web-side MVC, the support for JavaBean components is dependent on the container. The Javabean,web container is used by rules in JSPs and servlets to provide consistent support, and when the servlet modifies the properties of the JavaBean, renders the data from the JavaBean component to the page and translates it back into HTML when responding to the user JSP page.

There are two questions: 1), how to use JavaBean. Direct use, JSP page with <jsp:usebean/> tag, servlet with new operator. Indirect use, and can also be transmitted via the built-in object request. Both of these approaches are still attributed to the model, because its nature has not changed.

2), from the beginning of this article to the present, I still think that the model in MVC is simply a data model, rather than some blog on the JSP do interface, servlet do process Control, JavaBean processing business logic. MVC is essentially a model layer decoupling view and controller for understanding the decoupling interface display (such as JSP) and business logic (such as a service), the independent controller control process, and the interaction of each layer.

4. Other MVC Development Framework SPRINGMVC

The above Java EE Native component Jsp+serlet+javabean can already develop Web programs in MVC mode. So why do most background development use SPRINGMVC, Struts2 these development framework?

  

1) Java EE native components, although the development of the program to provide support, but in the face of the current changing needs, the development process is complex, duplicate code many. and SPRINGMVC provides better package components and environment support, faster and more concise for program development. It is obvious from above that the coupling between components is even lower. In particular, the view layer is separated from the other layers by SPRINGMVC, allowing the program to be truly separated from the front and back.

2) The SPRINGMVC framework provides a more flexible program architecture that adapts to the current development of the program. You can also return JSON, XML, Excel, PDF, and more, if you can not only respond to user JSP pages.

3) SPRINGMVC Framework, you can also freely choose to integrate with other frameworks, such as spring, MyBatis and so on, to provide better support for the development of the program.

4) The framework itself, which supports custom components for specific program architectures, will be more likely to be programmed. Depending on the interface provided by the framework, users can customize components such as Handeradapter, Viewresolver, and so on to provide support for controller and view changes.

5. MVC's derivative------MVP mode

  

MVC effectively reduces the interface between the display layer and the business logic layer of the coupling, accurate is the web-side MVC pattern, for the desktop side, MVC implementation is a bit complex, view at the same time rely on the controller and model, especially the View-model with the observer pattern implemented. The MVP evolved from MVC, but there's a big difference:

1) View update mode is different. In MVC, the model calls the view's interface refreshed, so view relies on model. The MVP view only relies on presenter, which is refreshed by the interface that presenter calls view.

2) The model of MVC is data models, just simple data encapsulation. And the model in MVP is the business models. This model is not the other model, this is the most confusing, but to distinguish, so use model (service).

6. MVC's derivation------the MVVM pattern

  

  

MVVP also evolved from MVC, which is somewhat similar to the MVP, although the refresh of the view is through "data binding". A typical implementation: the front-end JS frame vue:

    

1) through event-driven, the display layer of large user operations to the JS logic layer

2) JS after the logical processing, modify the data domain

3) The data field and the display layer HTML are bound, so the results are automatically flushed to the display layer HTML

7. Summary

Regardless of the desktop or Web side, one-time program interaction can be abstracted as:

    

    

1) Request: The desktop side through the event monitoring submission; The Web front end is submitted to the JS Logic layer via event-driven, and the backend is accepted through protocols such as HTTP, HTTPS, Web service, etc.

2) Processing: The controller invokes the interface to handle the business logic and the overall control of the entire operation process.

3) Response: The response content includes pages and data, there may be only pages such as static Web pages, there may be only data such as JSON.

The main differences between the various modes are summarized from the 3rd step above--response:

Desktop-side MVC, which is implemented by the observer pattern, refreshes the interface with the interface of the model call view.

Web-side MVC, through the Web container, SPRINGMVC and other three-party library support, the Modle rendering to view and return to the user display.

MVP mode, used for desktop or Android and other development, by presenter Call View interface Refresh interface.

The MVVM pattern, which is used for web front-end development, refreshes page data through data binding.

A summary of the development and application of 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.