I. Synthesis (composite) model
A synthetic pattern is sometimes called a partial-integral mode (part-whole). The compositing pattern organizes objects into a tree structure that can be used to describe the relationship between the whole and the part. A compositing pattern enables a client to treat a simple element to the same as a composite element.
From the story of the monk
This is a childhood my grandmother told the story: once there was a mountain, there is a temple in the mountains, there is an old monk in the temple to tell the story of the young monk, tell what story? Once upon a time there was a mountain, and there was a temple in the mountains ... How many times does Grandma's story cycle, depending on how long you fall asleep? In the story there are mountains, temples, monks and stories. Therefore, there are two kinds of characters in the story: one has no other role in it, and the other has other roles inside.
The tree structure of the object
A tree structure consists of two nodes: branch nodes and leaf nodes. Branch nodes can have child nodes, while a leaf node can not have child nodes. In addition to the root node, other nodes have and only one parent node.
Note: A branch node can be without any leaves, but because it has the ability to carry leaves, it is still a branch node, not a leaf node. A leaf node is never possible with a child node.
Ii. Overview of the synthesis model
The class diagram shown in the following illustration omits the details of each role.
As you can see, the class diagram structure above involves three roles:
Abstract widget (Component) role: This is an abstract role that provides an interface to the objects participating in the combination. This role gives a common interface and its default behavior.
Leaf component (leaf) role: A leaf object representing a group of participants. A leaf object has no subordinate child objects.
The branch component (composite) Role: Represents an object that participates in a combination of objects with a child, and gives the behavior of the tree Component object.
As you can see, objects of the composite type can contain objects of other component types. In other words, composite type objects can contain other types of branches (composite) or leaf (leaf) type objects.
The implementation of the synthetic pattern is divided into two forms, namely Safe mode and transparent mode, according to the difference of the implemented interface. The compositing pattern can not provide the parent object's management method, but the compositing pattern must provide the management method of the child object (such as: Add, remove, getchild, etc.) in the appropriate place.
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 synthetic model.
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. The leaf class object cannot have the next level of objects, so the Add (), remove (), and Getchild () methods have no meaning, do not make mistakes at compile time, and only go wrong 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.