This is the 17th day of my participation in the August Text Challenge.More challenges in August

Today we come to study together is not commonly used, but learning its principle can help us get more complex structure on a mode of thinking —- combination mode. I’m going to talk a lot about Java design patterns this month. Click on your avatar and follow my column. I’ll keep updating.

Singletons of design patterns

Factory patterns for design patterns

The Builder pattern of design patterns

Proxy patterns for design patterns

Visitor patterns for design patterns

Adapter pattern for design pattern

Design pattern of the command pattern

Java state pattern | monitoring state change anytime and anywhere

Java observer pattern | how to notice things change

Java mode | how to record history information memorandum

Java iterator pattern model | how to access each element

Java the flyweight pattern | how to share the object

Java interpreter pattern | custom rules implementation logic

The Java bridge model | abstract binding with different implementations

Java facade pattern | how to improve the high availability of the interface

Updates continue at……

Without further ado, let’s get to the point

Portfolio model

The combined pattern is relatively easy to understand. For example, the index design of MySQL uses the combined pattern design of B+ tree algorithm, which greatly improves the performance of data query.

Its official definition: a hierarchy that combines objects into a tree structure to represent the entire section. Composition mode allows users to treat single objects and combinations of objects uniformly.

Note: There are two key points in this definition: layering with a tree structure, and simplifying operations through uniform treatment, which are at the heart of the composite pattern

The common company statistics multiple dimensions of personnel wages information, main points department, post here, if a single wage a statistical information will be time-consuming, laborious but if we will personnel salary information in accordance with the organization structure to build into a “tree”, then according to certain classification label (for example, departments, post)

Let’s look at a problem:

From the figure, we can see that there are three key roles in the composite pattern:

  • Abstract components: Define unified operations that need to be implemented.

  • Composite node: Represents a composite object that can contain multiple nodes, meaning that there can be other composite nodes or leaf nodes underneath it.

  • Leaf node: Represents an atomic object, meaning that there are no other nodes underneath it

The most common structure of the composite pattern is the tree structure, which can be easily constructed from the above three roles. For example, there is a general manager in a company, and under the general manager there are managers, secretaries, deputy managers, etc., while under the manager there are team leaders, developers, etc. The structure diagram is roughly as follows:

Is it clear at a glance? That’s the tree. That’s the combination.

Let’s take a look at the code from the diagram above

The code shown

// Abstract the component
public abstract class Component{
     public abstract void operation(a);
}



// Leaf node
public class Leaf extends Component{
    @Override
    public void operation(a) {
        // Put the leaf operations here}}// Combine nodes
public class Node extends Component {
    private List<Component> myChildren;  // Store the list of child nodes
    @Override
    public void operation(a) {
        for(Component component: myChildren) { component.operation(); }}}Copy the code

The implementation of this code looks very simple, but there is one area where we often get confused: the for loop under the operation() method in Node. Most of the time, we think of this for loop as a fixed implementation of code, but this is not true. In fact, the point of the for loop here is to iterate over all the children of the composite node, not just loop.

From the above analysis, we can see that the composite pattern encapsulates the following changes:

  • The difference between leaf node and combination node;

  • Real data structures (trees, rings, networks, etc.);

  • Algorithms that traverse real structures (e.g., breadth-first, depth-first, etc.)

  • The strategy used by the structure (e.g., to aggregate data, or to find the best/worst nodes, etc.).

OK. Today’s combination mode is here, let’s summarize the following, the actual scene code I have not thought of a good scene code, I will post it later, you can pay attention to me, dry goods will continue to output

conclusion

Take a look at the advantages of using composite mode:

  • First, you want a set of objects to be managed in some sort of hierarchy, such as folders and files, items under orders, and so on. Trees have a natural hierarchical nature that allows us to understand the structure of multiple objects naturally.

  • Second, objects in complex structures need to be handled in a uniform manner, such as creating files, deleting files, moving files, etc. When working with files, we don’t really care how folders and files are organized and stored, as long as we can manipulate the files correctly, and the composite pattern is a great way to organize complex structures while operating according to the uniform behavior defined.

  • Third, the ability to quickly extend object combinations. , for example, under the orders of mobile phone product node can freedom articulated different classification of mobile phone (brand, such as huawei, apple), and you can also according to the characteristics of the goods (e.g., price, picture, businesses, functions, etc.) freely to hook up again in the new node combination, and when can start on the mobile phone search, increasing the node type, until find the right mobile phone products.

There are drawbacks. Note when you use:

1. A lot of run-time checking is required, which increases code complexity.

2. The system performance may be affected if a wrong traversal algorithm is encountered.