Design Pattern -- Structure Pattern -- combination pattern -- Java

Source: Internet
Author: User

Composite extends into tree structures to represent part-whole hierarchies. Compositelets clients treat individual objects and compositions of objects uniformly combines objects into a tree structure to represent the "part-whole" hierarchy. C o m p o s I t e makes the use of a single object and a combination object consistent applicabilityyou want to represent part-whole hierarchies of objectsyou want clients to be able to ignore the difference between compositions of objects and individual objects. clients will treat all objects in the composite structure uniformly you want to represent the part of the object-the overall hierarchy you want the user to ignore the differences between the combined object and a single object, the user will use all objects in the composite structure in a unified manner: UML participant component (Graphic) declares the interface for ob Jectsin the composition. implements default behavior for theinterface common to all classes, as appropriate. declares an interface for accessingand managing its child components. (optional) defines an interface foraccessing a component's parent in the recursive structure, and implements it ifthat's appropriate. leaf (Rectangle, Line, Text, etc .) represents leaf objects in thecomposition. A leaf has no c Hillit. defines behavior for primitiveobjects in the composition. composite (Picture) defines behavior for componentshaving children. stores child components. implements child-related operationsin the Component interface. clientmanipulates objects in thecomposition through the Component interface. in the drawing example of the security and transparent security patterns, Line, Circle, and Rectangle belong to Leaf, and only Picture has the add, remove, and getChild methods. Therefore, it is safe for Client calls and will not be abused. However, the Client must clarify which ones are Leaf and which are Composite transparent, leaf also has the add, remove, and getChild methods. Therefore, the client can ignore the leaf nodes and Composite nodes. However, if the node is incorrectly used during running, a running exception is thrown, for example, add a node to a leaf node. During implementation, we can throw a runtime exception in the add, remove, and getChild methods of the leaf node to avoid Client-side error calls. The two implementation methods need to be weighed based on requirements. If you are interested in SWing --- Composite Patterns, see the jdk documentation to identify whether it is a transparent implementation method or a secure implementation method.

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.