Viewing design Patterns from Java class libraries (4)

Source: Internet
Author: User

In the previous section, two structured patterns were introduced: Bridge and decorator. This part of the content will be followed by the above explanation to continue our design pattern tour.

This part, in addition to the introduction of a structured composite mode, there will be two behavioral patterns. In fact, in the previous context, we have come to terms with behavior patterns: Observer and command are two typical behavioral patterns. Behavioral patterns are more focused on the allocation of responsibilities between algorithms and object building, that is, it focuses more on the semantics of collaboration between objects such as this pattern system, and on the flow control of communication between objects.

Composite Mode

There is no doubt that the Component-container system in AWT is an example of a good composite model. Container inherits from component, and container can contain more than one component because container is actually component, so container can also contain container. In this way, a tree-like hierarchy is formed by combining the objects of the component-container structure. That's what composite mode does.

Composite mode is proposed to simplify programming, generally in programming, if the strict distinction between component and container, sometimes bring a lot of inconvenience, and these are often not necessary. For example, I want to put a component in a Container, I do not need to know whether this component is a Container, or is a general component, in the parent container to do is to record a A component reference that calls the component drawing method to display the component when needed. When this component is indeed a container, it can complete the display of the container by container the overloaded rendering method and pass the drawing message to its child objects. That is, for a parent container, it doesn't care if its child object is a component or a container. It needs to treat component and container in a unified way.

Figure 11: Class diagram for composite mode

The composite model is simpler and is not complicated to realize, but it has some limitations. For example, when dealing with trees, we often need to deal with three types of objects: subtree, page node, and non-page node. In composite mode, the distinction between subtree and non-leaf nodes is not obvious, but they are combined into a composite object. And in the composite model given by GOF, the method of adding, deleting subnodes and so on belongs to the composite object, which is placed in the Component object, although it can be distinguished in the realization, but it is easy to cause some conceptual misunderstanding.

From the above, we can propose an improved composite pattern by introducing the subtree object, which separates the subtree from the Non-leaf node, as shown in the following illustration:

Figure 12: A variant of the composite pattern

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.