definition
Name: Composite structure: intent: combine the object into a tree structure to indicate the hierarchy of "partial-overall". Composite enables users to have consistency for a single object and combined object. Applicability: You want to indicate part of the object - the overall hierarchy. You want users to ignore different combined objects and individual objects, and users will unify all objects in the combined structure. effect:
Classic Case 1, Java AWT Component-Container has no doubt that the Component-Container system in the AWT is a good example of a good Composite mode. Container inherits in Component, and the Container can include multiple Component because the Container is actually Component, so the container can also contain Container. Thus, a tree hierarchy is formed by combining the object of the Component-Container structure. This is also what the Composite mode is done. Composite mode is to simplify programming. Generally, when programming, if strictly distinguished Component and Container, sometimes it will bring a lot of inconvenience, and these often do not need. For example, I want to place a Component in a Container, I don't need to know this component is a Container, or a general component, what to do in the parent container, just record a Component's reference, in need Time to call the COMPONENT to display this component. When this Component is indeed a Container, it can complete the display of this container by the Container reload, complete the display of this container and pass the drawing message to its sub-object. That is to say, for a parent container, it is not concerned about it, and its child object is a Component, or a Container. It needs to treat Component and Container. 2, JUnit Best Practices 1, Composite Advantages and Disadvantages: For Composite mode, perhaps the attention of people will focus on how it implements combined objects. However, Composite is most important that users don't care whether combined object or a single object, the user will process in a unified manner, so the base class should be a common interface proposed from a single object and a combined object. Disadvantages: The biggest problem with Composite is to limit the components in the combination. Use improved Composite mode to ease this contradiction, see Reference Articles 2. Related Mode 1, Visitor / Iterator When you collectively or traverse your elements in Composite, you often use Visitor mode and Iterator mode reference article 1, design mode composite (combination) http://dev.9cbs.net/Article /52/52259.shtm2, design mode from Java class library (Composite, Strategy, Iterator) http://www.sawin.com.cn/doc/sd/uml/classlib.htm3, Diagnostics Java Code: Depth Priority Access Distribute http://www-900.ibm.com/developerWorks/cn/java/j-diag/part20/index.shtml