Tree Structure processing-combination mode (5)

Source: Internet
Author: User
11.5 company organizational structure

When learning and using the combination model, sunny software developers found that tree structures are everywhere. For example, Sunny's organizational structure is "a standard tree", as shown in 11-8:

Figure 11-8 sunny organization chart

In the sunny software company's internal office system sunny OA system, there is a tree menu corresponding to the company's organizational structure, the administrative staff can send a notification to all levels of units, these organizations can be a department of the company, a branch, or a branch of the company. You only need to select a root node to send notifications without worrying about the specific implementation details. Isn't this the "specialty" of the combination model? As a result, Sunny's developers drew a structure diagram 11-9:

Figure 11-9 Sunny's organizational structure combination model

In Figure 11-9, the "unit" acts as the abstract component, and the "Company" acts as the container component, "R & D department", "Finance Department", and "Human Resources Department" act as leaf components.

 

Thoughts

How do I code the "Company" class in Figure 11-9?

11.6 Summary of combined modes

The combination mode uses the object-oriented idea to construct and process the tree structure. It describes how to recursively combine container objects and leaf objects to achieve simple implementation and good flexibility. Because a large number of tree structures exist in software development, the combination mode is a frequently used structure design mode. The AWT and swing packages in Java SE are designed based on the combination mode, these interface packages provide you with a large number of container components (such as container) and member components (such as checkbox, button, and textcomponent). The structure is 11-10:

Figure 11-10 structure of AWT Combination Mode

In Figure 11-10, the component class is an abstract component. The checkbox, button, and textcomponent are leaf components, while the container is a container component. There are many leaf components contained in AWT, the space limit is not listed in the figure. A container component can contain leaf components, and can continue to contain container components. These leaf components and container components form a complex GUI together.

In addition, it has been widely used in XML parsing, organizational structure tree processing, file system design, and other fields.

1. Main advantages

The main advantages of the combination mode are as follows:

(1) The composite mode clearly defines hierarchical complex objects, indicating all or part of the objects. It allows the client to ignore the differences between layers and control the entire hierarchy.

(2) The client can use a composite structure or a single object. It does not have to worry about processing a single object or the entire composite structure, which simplifies the client code.

(3) adding new container components and leaf components in the combination mode is convenient, and there is no need to make any changes to the existing class libraries, complying with the "open and closed principle ".

(4) The combination mode provides a flexible solution for Object-Oriented Implementation of the tree structure. Through recursive combination of leaf objects and container objects, a complex tree structure can be formed, however, tree structure control is very simple.

2. Main disadvantages

The main disadvantages of the combination mode are as follows:

When adding new components, it is difficult to restrict the Component Types in the container. Sometimes we want a container to have only some specific types of objects. For example, a folder can only contain text files. When using the combination mode, these constraints cannot be imposed by the type system, because they all come from the same abstract layer, in this case, they must be implemented through type check during runtime, which is complicated.

3. Applicable scenarios

You can consider using the combination mode in the following cases:

(1) In a layered structure with a whole and a part, the client can treat the differences between the whole and the part in a consistent manner.

(2) A tree structure needs to be processed in a system developed using object-oriented language.

(3) In a system, leaf objects and container objects can be separated, and their types are not fixed. New types need to be added.

 

Exercise

Sunny software company wants to develop an interface control library. There are two types of interface controls: unit controls, such as buttons and text boxes, and container controls, such as forms and intermediate panels, use the combined mode to design the interface control library.

 

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.