Java and mode Learning Series-synthesis Mode

Source: Internet
Author: User

 

Structure of composite mode

The merging mode organizes an object into a tree structure and can be used to describe the relationship between the whole and the part. The merging mode allows the client to view elements as the matching elements. Use the merging mode in the following situations:

1. You need to describe the parts of the object and the overall hierarchical structure. 2. The client must ignore the differences between the individual and composite components. The client must treat all components equally, including individual components and composite components.

The merging mode can be either safe or transparent.

Transparent

Declare all methods in component to manage subclass objects, including add (), remove (), and getchild () methods. The advantage of this is that all component classes have the same interfaces. In the client's view, the difference between a leaf object and a synthetic object disappears at the interface level, and the client can treat all objects equally. This is the synthesis mode in transparent form. Its disadvantage is that it is not safe, because leaf objects and synthetic objects are essentially different. Leaf objects cannot have objects of the next level. Therefore, the add (), remove (), and getchild () methods are meaningless.

 


Security

Declare all methods used to manage subclass objects in the composite class of the tree branch structure. This approach is safe. The disadvantage is that it is not transparent, because the leaf and synthetic classes will have different interfaces.

II. Application of the synthesis mode in Java

Since the graphical components of AWT and swing are built on the container class and component class in the AWT library, we can see from the following AWT synthesis mode class diagram that the buttons and checkbox are tree components, the container is a branch component. As shown in:

In the container class, there are corresponding methods for Operation aggregation, but in the component class, there is no such method. That is to say, the synthesis mode used in AWT is the synthesis mode of the security situation.

 

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.