The combination mode of C + + implementation of design pattern

Source: Internet
Author: User

Problems to solve:

We use the file system of the PC, in fact, is our data structure in the tree structure, we deal with each node in the tree, in fact, regardless of whether he is a leaf node or root node, because their member functions are the same, this is the essence of the combination mode. He blurs the concept of simple elements and complex elements, and client programs can handle complex elements like simple elements, enabling client programs to decouple from the internal structure of complex elements.

Group objects into a tree structure to represent a "partial-whole" hierarchy. Combined mode enables users to have consistency in the use of individual objects and grouped objects.

Note: The leaf node in the tree structure also has the left and right children, but his children are empty.

Overview

The implementation of the combinatorial pattern is divided into two forms according to the difference of the implemented interface, which is called safe mode and transparent mode respectively. Combined mode does not provide the parent object's administrative method, but the combination mode must provide the administrative methods (such as Add, remove, getchild, etc.) for the child objects where appropriate.

Transparent way

As the first option, declare in component all the methods used to manage the subclass object, including Add (), remove (), and the Getchild () method. The advantage of doing this is that all the widget classes have the same interface. In the client's view, the difference between the leaf class object and the composite object is at least disappeared at the interface level, and the client can treat all the objects as equals. This is the transparent form of the combination mode.

The disadvantage of this choice is that it is not safe enough because there is a difference in nature between the leaves and the objects of the composite class. Leaf objects cannot have the next level of objects, so the Add (), remove (), and Getchild () methods do not make sense, and are not error-prone at compile time, but will only go wrong or be identified at run time.

Safe way

The second option is to declare in the composite class all the methods used to manage the subclass object. This is a safe practice because the object of the leaf type simply does not have the means to manage the subclass object, so if the client uses these methods for the leaf class object, the program will fail at compile time.

The disadvantage of this choice is that it is not transparent enough because the leaf class and the composite class will have different interfaces.

These two forms have their advantages and disadvantages and need to make a decision based on the specific circumstances of the software.

Class diagram structure and sample implementation:

In this paper, we give the class diagram structure of the security mode and the example implementation, the transparent way is the Add ()/remove ()/getchild () of the leaf node is realized, but meaningless realization. Most applications are based on transparent mode, because the code can be reused.

The combination mode of the safe way:

This form involves three roles:

Abstract widget (Component) role: This is an abstract role that defines the common interface and its default behavior for the participating objects, and can be used to manage all of the child objects. In the safe synthesis mode, the component role is not defined as the method of managing the child object, which is given by the tree Component object.

Leaf component (leaf) role: A Leaf object is an object that has no subordinate child objects, defining the behavior of the original object participating in the composition.

Branch Widget (composite) Role: An object that represents a subordinate child object that participates in a group. The Branch object gives all the methods for managing child objects, such as Add (), remove (), Getchild (), and so on.

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.