JAVA basics: MVC reduces programming complexity

Source: Internet
Author: User
JAVA basics: MVC reduces programming complexity-Linux general technology-Linux programming and kernel information. The following is a detailed description. MVC

With the rapid development and popularization of object-oriented languages (such as Java), more and more programmers begin to use these languages in application development. However, the original development language (that is, an operation-oriented development language such as C ++) cannot exit the stage of history in a short time, therefore, the coexistence of object-oriented languages and traditional operational-oriented languages has emerged. Using two different languages (mixed language design) in design applications brings about many new problems compared to using only one language in the past, complexity is the most common problem in hybrid language design. Next we will discuss what may lead to increased complexity in mixed language design, and how to reduce and eliminate these complexity.

Complexity

Complexity is the biggest headache in the application development process. When a feature is added to an application, its complexity usually increases exponentially. This complexity often causes program development to fail. This is why many applications only have Beta versions, but not the official version.

Experts divide the complexity of the application development process into two categories: non-essential and essential ). The inherent complexity is the inevitable complexity for solving the target problem. The non-essential complexity is the complexity produced by the selection of inappropriate development tools and design tools. For a function-determined program, the essence of complexity is fixed, but not the essence of complexity is unlimited. Therefore, to develop an application smoothly, we need to minimize non-essential complexity.

Features of OOD

Object-Oriented Design (OOD) breaks down a program into a series of elements designed based on specific objects. The behavior and data of these specific objects are packaged in a program unit called class. An application creates one or more examples of these classes, also known as "object )". Classes are combined by creating relationships between objects.

OOD allows developers to use two main methods to control the increase in complexity. First, OOD defines strict exit semantics, which allows developers to hide Implementation Details and clearly describe what other objects can access. This information is hidden so that most of the code can be modified without affecting other objects.

Second, OOD divides the relationships between objects into four categories: inheritance, inclusion, use, and coordination. The proper use of these relationships can greatly reduce the essential and non-essential complexity of the application development process. For example, inheritance is the main factor that can be reused in object-oriented design. This reuse is achieved through code sharing and polymorphism. This reuse can greatly reduce the essential complexity of the application. Allow a class to ignore the inclusive class when using the package container ). This simplification allows designers to greatly reduce the non-essential complexity of applications.

Insufficient visual interfaces in OOD

Many programs require visual interfaces, which are composed of dialog boxes, menus, and toolbar. The addition of these visual interfaces will introduce the disadvantages of OOD design, so that a good object-oriented design will go to the opposite. The visualized interface has three attributes that may cause problems for application development.

First, visualized interfaces improve the traditional operation-oriented topology. The user generates interface events, such as button selection and list box selection, which are driven by a module of the program and used to operate static data. In the design, the combination of the operation-oriented topology and an object-oriented design will lead to a large amount of mixing between objects.

Second, the user interface usually requires many different displays for the same information. For example, a customer selection list can contain a customer's name, phone number, and the names of many other customers.

After you select a specific customer, his/her name, phone number, and other related information will be displayed in detail.

In addition, a simple program may have different user interfaces. If a bank account system has an interface for a cashier to access account balance, deposits, and withdrawals, the supervisor's interface contains additional information and adds the account management function. These different interfaces can easily lead to class extension.

Finally, the visual interface will be greatly changed throughout the design phase. These changes include completely rescheduling the interaction between users and the system. These changes of visual interfaces increase the complexity of application development even in the best design.

MVC makes up for the shortcomings of visual interfaces/OOD

Model/interface/Controller (Model/View/Controller, MVC) programming technology allows a developer to connect a visual interface to an object-oriented design, at the same time, we can avoid the issues discussed above. MVC was initially designed for the Smalltalk language. MVC separates object-oriented design from visual interfaces by creating the following three layers:

Model: The Model contains all the behaviors and data required to complete the task. A model generally consists of many classes and uses object-oriented technology to create programs that meet five design goals.

View: an interface is a visual element of a program, such as a dialog box, menu bar, and toolbar. The interface displays the data provided from the model. It does not control the data or provides behaviors other than display. A single program or model generally has two interface behaviors.

Controller: the Controller maps the model to the interface. The controller processes user input. Each interface has a controller. It is a state machine that receives user input, creates or modifies an appropriate model object and will be modified in the interface. The Controller is also responsible for creating other interfaces and controllers when needed.

The Controller always determines which interfaces and model components should be active at a given time point, and is responsible for receiving and processing user input, any changes from user input are sent from the Controller to the model.

The interface displays data from objects in the model. Changes to these objects can be completed either through user interaction. For example, a Web browser is responsible for receiving information from the object collection and Assembly stack on the page. There must be some way to change the interface data of these objects. There are two ways to update the interface when the model changes.

In the first method, the interface can tell the model which objects it is monitoring. When any of these objects changes, a message is sent to the interface. The interface receives and updates the information accordingly. To avoid the shortcomings we discussed above, the model must be able to support many different Display Interfaces without modification.

The second method does not directly connect the interface to the model. Its controller updates the interface when the model changes. The controller monitors Model objects or observer methods to detect changes in the model. This method does not need to understand the model knowledge of the interface, so the interface can be used across applications.

Advantages of using MVC

MVC eliminates most of the difficulties related to user interfaces and object-oriented design in the following three ways:

First, the Controller tracks and processes operation-oriented user events through a state machine. This allows the Controller to create and destroy objects from the model when necessary, and isolates the operation-oriented Topology from the object-oriented design. This isolation helps prevent object-oriented design from moving toward the opposite.

Second, MVC separates user interfaces from object-oriented models. This allows you to use many different display methods without modifying the same model. In addition, if the model update is completed by the Controller, the interface can be used across applications.

Finally, MVC allows the user interface of the application to perform major changes without affecting the model. Each user interface changes only the controller, but since the controller contains very few actual behaviors, it is easy to modify.

Object-oriented designers must be very careful when adding a visual interface to an object-oriented design, because the operational-oriented topology of visual interfaces can greatly increase the design complexity.

The MVC design allows a developer to isolate a good object-oriented design from a user interface, allowing multiple interfaces to be easily used in the same model, in addition, the interface can be modified in the implementation phase without modifying the corresponding model.

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.