Compound Pattern)

Source: Internet
Author: User

1. What is composite mode?

In form, the composite mode is indeed a combination of multiple modes, but satisfying this condition is not necessarily a composite mode. Pay attention to its definition:

Combine multiple models to form a "Framework" to solve general problems

When it comes to the "Framework", the most likely thing to think of is MVC, but MVC is indeed a classic composite model.

Ii. MVC and composite mode

Roles and responsibilities of model, view, and controller:

The difference between control logic and application logic (algorithm logic) must be emphasized here:

    • The so-called control logic is to determine what object and method should be called in the current scenario
    • The application logic refers to the internal implementation of the specific methods of a specific object (a complicated algorithm or a series of specific processes)

(For details, the view actually contains a bit of control logic (which controller should be called based on user actions). Of course, we usually ignore this logic)

The biggest advantage of MVC is the separation of view and model in the presentation layer, which achieves loose coupling in design (response to changes) and code reuse (view can be changed at will, you just need to change the control logic in the new view)

-------

As mentioned above, MVC is a composite mode. Let's take a look at the patterns that composite it:

    • Observer mode: Both V and C are m observers (the model status update should notify V to update the view in time, or notify C to perform corresponding logic processing)
    • Policy mode: C is the "policy" of V, so the control logic contained in V is "Select policy", that is, select controller Controller
    • Combination Mode: V's own implementation applies the combination mode (call the repaint method of the top-layer container, and all components in the container will be re-painted)

MVC applies multiple modes and can better solve general design problems. Therefore, it is called composite mode.

Iii. MVC of traditional MVC and Java local programs

From the above we can see that the specific application logic in traditional MVC is included in M. That is to say, our model object not only has a series of attributes (and getter, setter) but also has related data processing methods.

This is different from the MVC in the Java local program. In the Java program, we create a package to layer the code structure. This is generally done as follows:

Note:

    • The VO package contains classes abstracted from various entities (the package name is also set as bean, but the meaning is the same. It only contains the attributes of each object and its corresponding getter and setter, excluding application logic)
    • Dao and core are both auxiliary layers of the service, and the three layers are mapped to controller.

The biggest difference between Java local program MVC and traditional MVC is that m in Java is more pure (clean) and only contains simple value objects, excluding any application logic, almost all logics are installed in the Controller (various concrete service classes)

4. Post

Mature frameworks that have applied the composite mode are far more than one MVC framework, but the rest of the frameworks are not currently available.

In the face of an unfamiliar framework, we may first analyze its internal implementation from the design perspective, such as the design modes applied, the functions of each layer, and the interaction between layers.

Understanding some basic design patterns helps us to quickly accept a framework. Only by understanding the internal implementation of the framework can we better control it ..

Compound Pattern)

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.