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.