Combinatorial mode is a common design pattern for maintainability-oriented programming. Simply put, a class contains a set of collection of the current class as attributes of the class. This is a certain similarity to recursion. His goal is to establish a tree hierarchy between objects of the same type, and an upper object can contain multiple downlevel objects. For example, a menu can have many options, and these options may contain the next level of menus, so that the relationship between menus and menus can be understood as a composite pattern. The corresponding UML diagram can be summarized as:
As can be seen from the UML diagram, a concrete class implements a structure, and the concrete class contains a field, which is composed of the implementation class of the same interface filed. This also makes up the same type of object that we said earlier, the tree hierarchy between which the upper object can contain multiple lower objects.
Below we give the actual code to realize:
Composite pattern of Java Design Patterns (combined mode)