Design Mode - MVC Mode

Above (Design Mode - Visitor Mode): https://blog.csdn.net/qq_16498553/article/details/106912484

Catalog

background

What is the MVC mode mode?

Roles:

Advantage:

Disadvantages:

What can MVC mode do?

MVC Pattern Class Diagram

Implementation Code

Source download:https://gitee.com/hong99/design-model/issues/I1IMES

Result

Source download:https://gitee.com/hong99/design-model/issues/I1IMES

Last

background

Remember that when you write jsp pages through serverlet, some code is written directly from the back. It's disgusting, everything is written directly from the back, including static code, and maintenance is extremely time-consuming and laborious. It's a bitter time, and mvc solves this problem well in the back.

What is the MVC mode mode?

The MVC mode represents the Model-View-Controller (Model-View-Controller) mode.This pattern is used for layered application development.

Roles:

Model: mainly responsible for database operations, as well as the implementation of related business logic, to provide data for view layer display.

View: mainly responsible for system interaction with users and data rendering;

Controller: Entry to a user request, receiving and returning the corresponding data model.

Advantage:

Clear structure, easy maintenance: Because each mvc layer has its own responsibilities, it is easy to manage and maintain, and the code reuse rate is also high;

Low coupling: Layers are separated from each other and have different responsibilities.

Disadvantages:

Because of the clear hierarchy of mvc, the related data flow is more complex to implement.

What can MVC mode do?

mvc is mainly a very good representation of java encapsulation, inheritance, polymorphism, reducing maintenance costs, clear responsibilities at all levels, and easy to manage. The main problem to solve is to put all the original logic into a single level, decouple into three layers of mvc with clear division of labor.

Personal understanding:

mvc, like some news we see on our mobile phone, sees the view layer, and the web address you request is the controller, which returns the news information through the back-end model layer.

MVC Pattern Class Diagram

 

Implementation Code

Source download: https://gitee.com/hong99/design-model/issues/I1IMES

/**
 * @Auther: csh
 * @Date: 2020/6/23 14:58
 * @Description: View layer
 */
public class NewsView {

    public void viewNews(NewsModel model){
        System.out.println("News Title:"+model.getTitle());
        System.out.println("News Content:"+model.getText());
    }
}
/**
 * @Auther: csh
 * @Date: 2020/6/23 14:53
 * @Description:News Information Model Layer
 */
public class NewsModel implements Serializable {
    private String title;
    private String text;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public NewsModel(String title, String text) {
        this.title = title;
        this.text = text;
    }
}
/**
 * @Auther: csh
 * @Date: 2020/6/23 15:01
 * @Description: Controller
 */
public class ViewController {
    private NewsModel model;
    private NewsView view;

    public ViewController(NewsModel model, NewsView view) {
        this.model = model;
        this.view = view;
    }

    public void viewNews(){
        view.viewNews(model);
    }
}

 

/**
 * @Auther: csh
 * @Date: 2020/6/23 15:02
 * @Description: mvc Users View News
 */
public class Client {

    public static void main(String[] args) {
        NewsModel newsModel = new NewsModel("Central News","12456497894");
        NewsModel newsMode2 = new NewsModel("Central News 2","12456497894");
        NewsView newsView = new NewsView();
        NewsView newsView2 = new NewsView();
        ViewController viewController = new ViewController(newsModel,newsView);
        viewController.viewNews();
        ViewController viewController2 = new ViewController(newsMode2,newsView2);
        viewController2.viewNews();
    }
}

Result

News Title: Central News
 News Content: 12456497894
 Headline: Central News 2
 News Content: 12456497894

Source download: https://gitee.com/hong99/design-model/issues/I1IMES

Last

MVC design mode and MVC framework are different, which needs your attention, because MVC design mode is just an idea, and MVC framework not only uses this idea, but also builds a framework based on this idea.Spring mvc, structs are derived from MVC design ideas.Most MVCs are presented in this mode in real work, such as spring mvc, spring boot, spring cloud, etc.

Reference article:

https://mp.weixin.qq.com/s/GuO5wdo2rJgvHH6mErLgqw

Tags: Spring JSP Database Java

Posted on Tue, 23 Jun 2020 12:11:02 -0400 by richardandrewle