python-Combination Mode

Source: Internet
Author: User

Description

The combined pattern is primarily used to deal with a class of objects that have "container characteristics"-they act as objects and can contain many other objects as containers. The combination mode treats the object of the container feature and the individual object equally. For example, when a computer processes an arithmetic expression, an arithmetic expression includes an operand, an operator, and another operand. The operand can be either a number or another expression. Thus 7+8 and (2+3) + (4*6) are valid expressions, and the computer first decomposes (2+3) + (4*6) into (2+3) and (4*6) the above two operands for further decomposition.

The combined pattern is also called part-the overall pattern, which is defined as follows: Grouping objects into a tree structure to represent a hierarchy of "partial" and "overall", which makes the user consistent with the use of individual objects and composite objects.

Instance:

Each company has its own organizational structure, the more large-scale enterprises, the more complex the organizational structure. In most cases, companies prefer to use a "tree" structure to organize complex corporate personnel relationships and inter-firm structural relationships. In general, the root node represents the company's highest administrative right unit, the branch node represents a department, and the leaf node will be used to represent each employee. A subtree of each node that represents the units managed by the Department represented by the node. Suppose a company with an HR department, a finance department and a Research and development department, and a branch company in the country, whose corporate structure can be represented as follows logic:

classCompany:name="'    def __init__(self, name): Self.name=namedefAdd (self, company):Pass    defRemove (self, company):Pass    defdisplay (self, depth):Pass    deflistduty (self):PassclassConcretecompany (company): Childrencompany=Nonedef __init__(self, name): Company.__init__(self,name) Self.childrencompany= []    defAdd (self, Company): Self.childrenCompany.append (company)defRemove (self, Company): Self.childrenCompany.remove (company)defdisplay (self, depth):Print('-'*depth +self.name) forComponentinchSelf.childrenCompany:component.display (Depth+1)    deflistduty (self): forComponentinchself.childrenCompany:component.listDuty ()classHrdepartment (company):def __init__(self, name): Company.__init__(Self,name)defdisplay (self, depth):Print('-'*depth +self.name)defListduty (self):#Perform Duties        Print('%s\t Enrolling & transfering management.'%self.name)classFinancedepartment (company):def __init__(self, name): Company.__init__(Self,name)defdisplay (self, depth):Print("-"* Depth +self.name)defListduty (self):#Perform Duties        Print('%s\tfinance Management.'%self.name)classRddepartment (company):def __init__(self,name): Company.__init__(Self,name)defdisplay (self, depth):Print("-"*depth+self.name)deflistduty (self):Print("%s\tresearch & Development."%self.name)

"""
In this example, the corporate structure is abstracted only by the company (Concretecompany) and the department (Department),
The company has the possibility of subsidiaries, the company also has its own department, the department is the ultimate leaf node.
Assume that the head office under the east of a branch, the East Branch under the northeast Company and Southeast Company, showing the company level,
and listing the responsibilities of each department in these companies, you can build the following business scenarios:
"""if __name__=="__main__": Root= Concretecompany ('Headquarter') Root.add (Hrdepartment ('HQ HR')) Root.add (Financedepartment ('HQ Finance')) Root.add (Rddepartment ("HQ R & R")) Comp= Concretecompany ('East Branch') Comp.add (Hrdepartment ('east.br HR')) Comp.add (Financedepartment ('east.br Finance')) Comp.add (Rddepartment ("east.br") ) Root.add (comp) comp1= Concretecompany ('northast Branch') Comp1.add (Hrdepartment ('northeast.br HR')) Comp1.add (Financedepartment ('northeast.br Finance')) Comp1.add (Rddepartment ("northeast.br") ) Comp.add (COMP1) comp2= Concretecompany ('Southeast Branch') Comp2.add (Hrdepartment ('southeast.br HR')) Comp2.add (Financedepartment ('southeast.br Finance')) Comp2.add (Rddepartment ("southeast.br") ) Comp.add (COMP2) Root.display (1) root.listduty ()

Printing results:

-headquarter
--HQ HR
--HQ Finance
--hq
--east Branch
---east.br HR
---east.br Finance
---east.br
---northast Branch
----northeast.br HR
----northeast.br Finance
----northeast.br
---southeast Branch
----southeast.br HR
----southeast.br Finance
----southeast.br
HQ HR Enrolling & transfering management.
HQ financefinance Management.
HQ R&dresearch & Development.
east.br HR Enrolling & transfering management.
east.br financefinance Management.
east.br R&dresearch & Development.
northeast.br HR Enrolling & transfering management.
northeast.br financefinance Management.
northeast.br R&dresearch & Development.
southeast.br HR Enrolling & transfering management.
southeast.br financefinance Management.
southeast.br R&dresearch & Development.

Advantages:

1, the node increase and decrease is very free and convenient, this is also a big feature of the tree-shaped structure;
2, all nodes, whether it is a branch node or leaf nodes, whether it is called a node, or call a node group, are very convenient.

Usage scenarios:

1. If you want to divide the object into parts-the overall hierarchy

2, you want users to ignore the combination of objects and a single object, the user will uniformly use all the objects in the composite structure.

Disadvantages

The combination mode uses the implementation class directly when defining leaves and branches, and does not conform to object-oriented interface programming. Conflict with dependency inversion principle.

python-Combination Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.