The 10th Chapter Structural mode-Combined mode

Source: Internet
Author: User

1. Definition of combination mode (Composite pattern)

(1) Combine objects into a tree structure to represent a "partial-whole" hierarchy. The combined mode makes the user consistent with the use of individual objects and composite objects .

(2) Structure and description of the combined mode

①component: Abstract component objects, which declare interfaces for objects in a composition, allow clients to access and manage the entire object structure through this interface, which provides a default implementation for defined functions.

②leaf: Leaf node object that defines and implements the behavior of the leaf object, no longer contains other child node objects .

③composite: A composite object that typically stores subcomponents , defines the behavior of those components that contain subcomponents, and implements the operations that are defined in the component interface that are related to the child components.

④client: A client that uses component interfaces to manipulate component objects inside a composite structure.

(3) Structure of a typical composite composite object

(4) Thinking of combination mode

① the essence of the combination pattern: unify the Leaf object and the combined object . It treats both the leaf object and the combined object as component objects, organically unifying the leaf object and the combined object.

② The purpose of the combo mode: to let the client not distinguish between the operation of a composite object or a leaf object , but in a unified way to operate.

There is a natural recursion in the ③ combinatorial pattern, where recursion is not often referred to as "method invocation itself" recursion. Instead, it refers to the recursive combination of objects .

2. Security and transparency--define the sub-component operation method in the component interface or the composite class?

(1) The realization of transparency (recommended way of use!) )

① the operation of the subcomponent is defined in the component , then the client only needs to face component, without concern for the specific component type, this implementation is the implementation of transparency. (See the previous class diagram structure)

② This transparency is at the expense of security , because some of the methods defined in component do not make sense for leaf objects, such as adding and removing child component objects. The client is unaware of these differences because it is transparent to the client, and it is unsafe for the client to invoke these methods.

③ to prevent clients from calling these methods with security issues, the default implementations are generally provided for these methods in component, usually by throwing an exception to indicate that this function is not supported.

(2) Implementation of security

① the operation of the management sub-component is defined in the composite , then the client uses the leaf object, because there is no operation to call, so it is safe.

② But the problem is that when the client is in use , it must distinguish between the composite and the leaf object .

(3) Choice of two ways of realization

① for combination mode, transparency is more important in security and transparency, after all, the function of the combined pattern is to make the user consistent with the use of the leaf object and the combined object.

② for security implementations, you need to distinguish between a composite object or a leaf object. Coercion is sometimes required for type conversion, but this is inherently unsafe, so in component, it is common to define a Getcomposite method to determine whether a leaf object or a combination object , so that before the use of the first judgment, the conversion will not appear security problems.

3. Parent component and ring reference issues

3.1 Parent Component Reference: Saving a reference to a parent component object in a child component Object

(1) Use the scene: If you delete a product type A, but if it has sub-type B, this will involve the processing of sub-types, is the total deletion, or the sub-type moved up one layer, that becomes b.setparent (a.parent);

(2) Reference definition : A reference to the parent component is typically defined in component . Both the combined object and the leaf object can inherit this reference.

(3) Reference Maintenance : In the implementation of composite, when the composition object is added to the child Component Object , the parent object is set, when the child Component object is deleted A reference to the parent component is reset (the Subassembly object that may be removed from the component involves a top-up problem).

3.2 Ring Reference

(1) Concept : In an object structure, an object contains child objects, or sub-objects of child objects, or child objects of child objects ... After the n-tier, the object itself is present in the contained sub-objects, thus forming a circular reference. For example, a contains b,b that contains C, and C contains a, which forms a circular reference.

(2) The combination mode is generally used when building a tree structure , usually to avoid the appearance of circular references (of course, some special needs, also need ring reference), otherwise it is easy to create a dead loop.

(3) Methods for detecting and processing circular references:

① records the path where each component starts from the root node, where an object occurs two times, which makes a ring reference.

② first sets up a field in component that is designed to record the path of the component from the root node to the component itself.

③ when adding a subassembly method for a composite object, first detects whether the child component being added appears in the path above, and throws an exception if a ring reference occurs.

(4) Description:

The loop detection method above the ① is simple, but does not take into account that if a Component object on a path is deleted, all the paths recorded by the child component objects of that component object are updated.

② can consider how to dynamically calculate the path , each time a component is added, the dynamic recursive look for the parent component, and then the parent component to find the parent component until the root component. This prevents a component from being deleted and the path changes and all related path records need to be modified.

4. precautions for use

(1) Should the sub-component list (list<component*>) be placed in the Component interface or the composite class?

① in most cases, a composite object holds a collection of child nodes. Because the leaf node is not a child component.

② However, the sub-component list can also be defined in the component interface (the Aps.net container class, that is, defined), which can be simply the operation of the leaf node. However, for leaf nodes, it can lead to wasted space , because the leaf node itself does not need child nodes, so only when the number of leaf objects in the composite structure is small, this method is used.

(2) Maximize component definition

Component the method in the two objects is the sum of the external methods , in other words, a bit of a hodgepodge of meaning, the component contains both the method required by the leaf object, but also the combination of objects required methods. This causes " interface pollution " and conflicts with the design principles of the class.

(3) Sub-component ordering

① when a subassembly object needs to be used in a certain order, such as when parsing a syntax tree, the design needs to take the index of the Component object into account and design the access and management interfaces for the child nodes.

② typically combine the iterator pattern to enable access to component objects in order.

5. Advantages and disadvantages of combination mode

(1) Advantages:

① High-level mode invocation is simple because it unifies the operation of the leaf object and the combined object.

The ② node is free to increase, as long as it finds its parent node, it is easy to expand and conforms to the open and closed principle.

③ can be combined into complex objects to form the class hierarchy of a unified composite object

(2) Disadvantages

It is difficult to limit the types of components in a composition, and when you need to detect component types, you cannot rely on the type of compilation period to constrain and must be dynamically instrumented during run time.

6. usage Scenarios for combined mode

(1) In general, the combined pattern is combined with a tree structure, which means that all functions that can be described or manipulated using the object Tree can be considered using a combination pattern, such as a container object in UI design, reading XML, or parsing a statement .

(2) If you want to represent the part of the object-the overall hierarchy, unify the whole and part of the operation, making the hierarchy implementation simpler, and using the hierarchy from the outside is easy.

(3) If you want to use all the objects in the composite structure uniformly, you can choose the combination mode.

The 10th Chapter Structural mode-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.