Combination Mode of Design Pattern 15

background

First of all, let's understand the "part-whole", this kind of relationship "part-whole" in real life is also very common. For example: colleges and schools, branches and head offices, books and bookcases, etc.

In software development, we often have to deal with simple objects and composite objects. We can use the combined mode to process single objects and composite objects consistently. The client does not need to know whether it is a single object or a composite object to be called. This versatility reduces the client code very well.

Is the above introduction more abstract? Don't worry, please continue to look down.

What is the combination mode

Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly. The use of objects is consistent.)

The combination mode mainly consists of 3 elements:

  • Abstract component (Component) role: Its main function is to declare public interfaces for leaf and branch components and implement their default behaviors.

  • Leaf component (Leaf) role: It is a leaf node object in the composition, it has no child nodes, and is used to implement the public interface declared in the abstract component role.

  • The role of the branch component (Composite): it is the branch node object in the combination, and it has child nodes. It implements the interface declared in the role of the abstract component, and its main function is to store and manage sub-components, usually containing  Add()、Remove()、GetChild() methods.

The structure diagram of the combined mode is as follows.

Combination mode

Code

Component

Leaf

Composite

Test code:

What does this test code do? We use it to traverse Leafthe content.

Traverse

The test results are as follows:

树叶Lvshen1:被访问!
树叶Lvshen2:被访问!
树叶Lvshen3:被访问!

Thinking about combination mode

Regarding the above code, it Leafis a single object, which Compositeis a compound object. For the client, what is called is Componentthat there is no difference between the whole and the part. CompositeIt can load both itself and it Leafonly needs a set of codes to be satisfied.

After using the combined mode, we can see that if we want to add a branch (Composite) node or a leaf (Leaf) node, is it easy? Just find its parent node. It is very easy to expand and conforms to the principle of opening and closing. It is very beneficial for future maintenance.

So when do we use it?

In the development, if we need to show "part-to-whole" scenes, such as tree menu, file and folder management, etc., we can use the combined mode.

Also, if you need to develop a management system, it may involve the development of personnel rank relationships. For example, the tree structure of general manager-department manager-employee.

In the e-commerce distribution system, the relationship between online and offline needs to be displayed. Combination mode can also be used.

 

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

  1. 回复"java" 获取java电子书;

  2. 回复"python"获取python电子书;

  3. 回复"算法"获取算法电子书;

  4. 回复"大数据"获取大数据电子书;

  5. 回复"spring"获取SpringBoot的学习视频。

  6. 回复"面试"获取一线大厂面试资料

  7. 回复"进阶之路"获取Java进阶之路的思维导图

  8. 回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

  9. 回复"总结"获取Java后端面试经验总结PDF版

  10. 回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

  11. 回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

 

Guess you like

Origin blog.csdn.net/wujialv/article/details/109483356