In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of structural composition patterns in Python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
I. combination mode
Composition, which combines multiple objects into a tree structure to represent the hierarchy of business logic. The combination mode enables users to use individual objects and composite objects consistently.
For example, if we describe the hierarchical structure of a company, then we use the office to represent the node, then the general manager's office is the root node, followed by the personnel office, the business office, the production office, and the finance office. there can be a small office under each office, and each office has attributes such as responsibility, number of personnel, salary and so on.
Advantages:
Defines a class hierarchy that contains basic objects and composite objects.
Simplify the Client code, that is, Client can use composite objects and individual objects consistently.
It is easier to add new types of components.
Second, application scenarios
It is used to design a hierarchical system and deal with the relationship between different levels.
Third, code example
Entity roles:
Abstract component (Component)
Leaf component (Leaf)
Composite component (Composite)
Client (Client)
Class ComponentBase: def _ init__ (self, name): slef.name = name def add (self, obj): pass def remove (self, obj): pass def display (self, number): passclass Node (ComponentBase): def _ init__ (self, name) Duty): self.name = name self.duty = duty self.children = [] def add (self, obj): self.children.append (obj) def remove (self, obj): self.children.remove (obj) def display (self, number=1): print ("Department: {} level: {} responsibilities: {}" .format (self.name, number) Self.duty) n = number+1 for obj in self.children: obj.display (n) if _ _ name__ = ='_ _ main__': root = Node ("General Manager's Office", "Chief Executive") node1 = Node ("Finance Department", "Corporate Financial Management") root.add (node1) node2 = Node ("Business Unit" "sell products") root.add (node2) node3 = Node ("production department", "production product") root.add (node3) node4 = Node ("sales business one department", "A product sales") node2.add (node4) node5 = Node ("sales business two departments" "Product B sales") node2.add (node5) root.display () on "sample Analysis of structural composition patterns in Python" ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.