In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "Composite combination pattern how to achieve", the content is detailed, the steps are clear, and the details are handled properly. I hope this "Composite combination pattern how to achieve" article can help you solve your doubts.
Composite (combined mode)
Composite (composition pattern) belongs to structural pattern, which is an abstract way to manage tree structure in a unified way.
Intention: to combine objects into a tree structure to represent a "part-whole" hierarchy. Composite enables users to use single objects and combined objects consistently.
As an example
If you do not understand the above intention introduction, it does not matter, the design pattern needs to be used in daily work, combined with examples can deepen your understanding, below I have prepared three examples to let you experience in what scenarios this design pattern will be used.
Company organizational relationship tree
The organizational relationship of a company may be divided into departments and people, in which people belong to departments, some people have subordinates, and some people do not have subordinates. If we uniformly abstract departments and people into organizational nodes, we can easily count the number of people, financial data and so on under a certain department, regardless of whether the current node is a department or a person.
Operating system folders and files
The folder and file of the operating system is also a typical tree structure. in order to recursively find the number or total size of files in the folder, it is best to abstract folders and files into files when we design them. In this way, each node has the same method to add, delete, and find child elements, regardless of whether the current node is a folder or file.
Components and containers for building the platform
The relationship between the container and the component is very small, and users often think that the container is also a kind of component, but when building a platform implementation, the container and the component are slightly different, the difference is that the container can nest child elements, but the component cannot. If you build a platform and divide components into containers and components, it will cause API to be split into two sets, which is not conducive to component developer maintenance and user understanding. A better design idea is to treat components and containers as components, and components are just special containers without child elements, so that components and containers can have the same API, unified understanding and operation.
Intention interpretation
Intention: to combine objects into a tree structure to represent a "part-whole" hierarchy. Composite enables users to use single objects and combined objects consistently.
It is easy to understand that combination means that although many objects are different, they are combined into a tree structure, then there must be a "part-whole" relationship between objects. The combination pattern requires us to abstract an object Component as a unified operation model. Leaf nodes and non-leaf nodes achieve all functions, even if there are no sub-elements of leaf nodes, in order to emphasize transparency. Still have methods such as getChildren, but always return null.
Structure diagram
Where Component is the object declaration interface in the composition, it generally implements all the interfaces of all public classes, and also provides an interface to manage its subcomponents.
Leaf represents a leaf node, and there are no child nodes, and the corresponding Composite is a node with child nodes.
As we can see, the combination pattern abstracts all the nodes in the tree structure uniformly. We do not need to care about the differences between leaf nodes and non-leaf nodes, but can shield these differences through the abstraction of combination patterns and deal with them uniformly.
Code example
The following example is written in typescript.
/ / Unified abstraction
Class Component {
/ / add child elements
Public add () {}
/ / get the name
Public getName () {}
/ / get child elements
Public getChildren () {}
}
/ / non-leaf node
Class Composite extends Component {
Public add (component: Component) {
This.children.push (component)
}
Public getName () {
Return this.name
}
Public getChildren () {
Return this.children
}
}
/ / Leaf node
Class Leaf extends Component {
Public add (component: Component) {
Throw Error ('Leaf node cannot add element')
}
Public getName () {
Return this.name
}
Public getChildren () {
Return null
}
}
Finally, we convert the operations on all nodes to Component objects, regardless of whether the object is Composite or Leaf.
Malpractice
The composition pattern carries on a layer of abstraction, which actually increases the complexity of the business in the complex system. If there is too much difference between Composite and Leaf, then the cost of understanding brought about by unified abstraction is high.
At the same time, Leaf has to implement some empty functions that only exist in Composite, such as add delete. Even if these methods are meaningless to them, unified invalid or error handling may be required at this time, so that the business layer really does not have to perceive their differences, otherwise add may fail, which essentially exposes the differences between nodes to the business layer.
After reading this, the article "how to realize the Composite combination pattern" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.