Java EE Basics: An understanding of MVC pattern and struts pattern

Source: Internet
Author: User
Tags data structures

The MVC approach is typically used in Smalltalk to establish a user interface. The design patterns contained in MVC can help you understand what we mean by "patterns."

MVC includes three types of objects, model is the Application object, view for its screen representation, controller defines the processing of user input (reaction) way. Until the MVC approach is applied, these three objects are typically combined, and MVC is used to separate them, providing flexibility and reusability for the design.

MVC separates the View and model objects by establishing the Subscribe/notify protocol between view and model. The view object must ensure that its representation reflects the state of the model object, and when the model object's data changes, the Model object notifies (Notify) The View object as a response to this behavior, and each view object gets an opportunity to make an update. This approach allows multiple view objects to provide different representations for a model object. You can also create a new view object for the model object without having to rewrite the model. The following illustration shows a model and three view:

On the surface, this example reflects a design that separates view and model. However, this design is suitable for a more general problem: reducing the coupling and sex between objects, so that when an object changes, it will not affect other objects, or even need to know the details of the implementation of other objects. This more general pattern is described in observer mode.

Another characteristic of the MVC approach is that the view object is nested defined. For example, a button's dashboard can be implemented by a complex view object that contains a nested button view object; The user interface of an object watcher can be made up of nested view objects that can be reset to the debugger. The MVC method uses the Compositeview class (view subclass) to support nested view, which behaves in the same way as the view object, and can be used in any situation where the view object can be used.

We can then treat composite view as a design (mode) as a way of dealing with one of its components. Similarly, this design abstracts out another, more general problem (the solution): In some cases we divide the objects into groups and deal with a group as if it were an object entity. In this way we use the composite design pattern to describe. It allows you to build hierarchies of classes, where some subclasses define the original object (such as a button), while other classes can define a composite object (Compositeview), which assembles the original object into more complex objects.

Similarly, MVC can change the way a view class responds to a user without changing its visual representation. You may want to change the way it responds to the keyboard, such as using a pop-up menu instead of a command key. MVC encapsulates this reaction mechanism as a control object (Controller). The controller has a class hierarchy that is easy to implement from an existing controller to create a variant-a new controller.

A View object implements a specific response strategy through an instance of a Controller object (instance). To implement different policies, you can simply replace the current instance with a different controller instance. You can even change the controller of the view at run time to change the response (policy) of the View object to user input. For example, a View object can be set to disabled, that is, no response to the user's input. To achieve this, simply let the controller ignore all input events.

This view-controller relationship is a typical example of a strategy design pattern. The so-called strategy is such an object, it represents an algorithm. This is especially useful when you want to replace an algorithm, whether it's a static replacement or a dynamic replacement, which may have many variables or have complex data structures.

Other design patterns are also used in MVC, for example, using the factory method pattern to describe the default controller class for the view, and the decorator mode to add scroll bars to the view. But the main pattern in MVC is the aforementioned observer, Composite, and strategy design patterns.

Related Article

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.