11.2 overview of combined modes
For a tree structure, when a method of a container object (such as a folder) is called, the entire tree structure is traversed to find a member object that also contains this method (which can be a container object, it can also be a leaf object) and call and execute it. The whole structure is processed using a recursive call mechanism. Because of the functional differences between container objects and leaf objects, you must treat container objects and leaf objects differently in the code that uses these objects, in fact, in most cases, we want to process them in a consistent manner, because the difference in the treatment of these objects will make the program very complicated. The combination mode is born to solve such problems. It allows the use of leaf objects and container objects to be consistent.
The combination mode is defined as follows:
Composite pattern: combines multiple objects to form a tree structure to indicate a hierarchy with a "Whole-part" relationship. The combination mode is consistent with the use of a single object (that is, a leaf object) and a combination object (that is, a container object). The combination mode can also be called the part-whole mode, it is an object structure mode. |
The abstract component class component is introduced in the composite mode, which is the common parent class of all the container classes and leaf classes. The client program component. The composite mode structure is 11-3:
Figure 11-3 Combined Mode Structure
The composite mode structure includes the following roles:
● Component (Abstract component ):It can be an interface or abstract class. It declares an interface for leaf components and container component objects. This role can contain the comments and implementations of the common behavior of all sub-classes. The abstract component defines methods for accessing and managing its child components, such as adding, deleting, and obtaining child components.
● Leaf (leaf component ):It indicates the leaf node object in the composite structure, and the leaf node does not have any subnodes. It implements the behavior defined in the abstract component. Methods for accessing and managing sub-components can be handled by exceptions.
● Composite (container component ):It indicates the container Node object in the combination structure. The Container node contains sub-nodes. Its Sub-nodes can be leaf nodes or container nodes. It provides a set for storing sub-nodes, the behavior defined in the abstract component is implemented, including the methods for accessing and managing the child component. In the business method, the Business Methods of the child nodes can be recursively called.
The key to the combination mode is to define an abstract component class, which can represent both the leaf and the container, and the client program the abstract component class, you do not need to know whether it represents a leaf or a container. You can perform unified processing on it.At the same time, an aggregation association relationship is also established between the container object and the abstract component class. The container object can contain both leaves and containers, so as to realize recursive combination and form a tree structure.
If the combination mode is not used, the client code will rely too much on the complicated internal implementation structure of the container object. Changes in the implementation structure of the container object will cause frequent changes in the Customer Code, it brings about the drawbacks of complicated code maintenance and poor scalability. The introduction of the combination mode will solve these problems to a certain extent.
The following uses simple sample code to analyze the usage and implementation of each role in the combination mode. For the abstract component role in the combination mode, the typical code is as follows:
Abstract class component {public abstract void add (component C); // Add the public abstract void remove (component C) member; // Delete the public abstract component getchild (INT I) member ); // obtain the public abstract void operation (); // business method}
Generally, the abstract component class is designed as an interface or abstract class, and the declarations and implementations of Methods common to all subclasses are placed in the abstract component class. For the client, it will program the abstract component without worrying about whether its specific subclass is a container component or a leaf component.
If the leaf component inherits the abstract component, the typical code is as follows:
Class leaf extends component {public void add (component C) {// exception handling or error prompt} public void remove (component C) {// exception handling or error prompt} public component getchild (int I) {// exception handling or error prompt return NULL;} public void operation () {// implementation of specific business methods for leaf components }}
As a subclass of the abstract component class, all methods declared in the abstract component class must be implemented in the leaf component, including business methods and methods for managing and accessing child components, however, leaf components cannot contain child components. ThereforeException Handling or error prompting is required when sub-component management and access methods are implemented in leaf components.. Of course, this will undoubtedly bring troubles to the implementation of leaf components.
If the abstract component inherits the container component, the typical code is as follows:
Class composite extends component {private arraylist <component> List = new arraylist <component> (); Public void add (component C) {list. add (c);} public void remove (component C) {list. remove (c);} public component getchild (int I) {return (Component) list. get (I);} public void operation () {// implementation of the specific business method of the container component // recursively call the Business Method of the member component for (Object OBJ: List) {(Component) OBJ ). operation ();}}}
All methods declared in abstract components are implemented in container components, including both business methods and methods used to access and manage Member child components, such as add () and remove () and getchild. When implementing specific business methods, container components act as containers and contain Member components, so they call the business methods of their member components.In the combination mode structure, container components can still contain container components. Therefore, recursive algorithms must be used to process container components.That is, the operation () method of the member component is recursively called in the operation () method of the container component.
|
Thoughts In the composite mode structure diagram, what results will be generated if the aggregate link is not from composite to component, but from composite to leaf, as shown in 11-4? Figure 11-4 Question structure of the Combined Mode |
|
[Author: Liu Wei http://blog.csdn.net/lovelion]