Composite of Java design patterns

summary

It is a structural mode, which organizes objects in a tree structure to represent the "part whole" hierarchy, so that the use of single objects and composite objects by the client is unique.

UML class diagram

The above class diagram contains the following roles:

Component: declare a public interface for objects participating in composition, whether it is composition or leaf node. Leaf: represents the leaf node object in the combination, and the leaf node has no child nodes. Composite: represents the objects with sub objects participating in the combination, and gives the behavior of branch construction;

Code example

Application scenario

1. It is required to represent the part overall hierarchy of the object. 2. If you want the client to ignore the difference between a composite object and a single object, the client will uniformly use all objects in the composite structure.

The composition pattern defines the class structure composed of leaf object and composite object; Make the client simple; It makes it easy to add or remove subassemblies.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>