Composite pattern of Java Design Patterns (combined mode)

Source: Internet
Author: User

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)

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.