Python3 is the most basic and simple implementation combination design pattern

The combination mode is to classify a category into a whole, and organize the relationship between multiple wholes, using a tree structure to describe all the wholes.

The general way of writing is to add multiple elements of the object under a category object, and the object is an element under other objects. To put it simply, a school has a headquarters, under which there is a teacher department and an admissions department; there are branch schools in the headquarters, as well as branch schools.

In my personal opinion, the use of design patterns is not limited to the solution of specific problems described in books. It requires a wealth of business experience in order to be applied flexibly. My ability is limited. Here is also a simple example for Explain that the additional flexible operation depends on the personal writing method.

First create a base class for the school:

#根
class root:
    name = ''
    def __init__(self, name):
        self.name = name
    def add(self, root):
        pass 

The initialization method of the above school base class is to receive a name, and there is an abstract method add.

After having a school base class, create a new school class and inherit the root base class:

#学校类
class School(root):
    childrenroot = []
    def add(self, root):
        self.childrenroot.append(root) 

In the above school class, the root base class is inherited, and the add method is implemented to receive the root value and add it to the childrenroot list, so that you can add child nodes under the current object, or add subordinate objects.

Create a new admissions office class and a teacher department class:

#招生办
class StudentAdmissionDept(root):   
    def __init__(self, name):
        self.name = name
#师资部
class FacultyDepartment(root):    
    def __init__(self, name):
        self.name = name

The above content is very simple, all inherited from root, just an initialization method.

Start to implement the call. First, create a new school headquarters object, and add an admissions office object and a teacher department object under this headquarters object:

root = School('总部')
root.add(StudentAdmissionDept('总部招生办'))
root.add(FacultyDepartment('总部师资部'))

After creating the new headquarters object, add the branch campus objects down. The department setting of the branch campus is the same as that of the main campus:

s1 = School('桂林校区')
s1.add(StudentAdmissionDept('桂林校区 招生办'))
s1.add(FacultyDepartment('桂林校区 师资部'))
root.add(s1)

After configuring the first Guilin campus, use the add method of root headquarters to add the current Guilin campus as a subordinate.

Create a few more campuses:

s2 = School('深圳校区')
s2.add(StudentAdmissionDept('深圳校区 招生办'))
s2.add(FacultyDepartment('深圳校区 师资部'))
root.add(s2)

s3 = School('广州校区')
s3.add(StudentAdmissionDept('广州校区 招生办'))
s3.add(FacultyDepartment('广州校区 师资部'))
root.add(s3)    

The above configuration method is the same as that of the first campus configured as the subordinate of the root headquarters campus.

Finally, use traversal to print out the name of each campus:

print('\n',root.name,'下级学校层级:\n')

for i in root.childrenroot:
    print(i.name)

The result is:
Insert picture description here
Because there is no typesetting, the above results do not have a good visual display level, and the rest only needs to typeset and output by yourself.

In the code writing corresponding to the current article, the last new campus and configuration information can be created by creating a new class, encapsulating the calling and output methods, and using the combined design pattern easily.

The complete code is as follows:

#根
class root:
    name = ''
    def __init__(self, name):
        self.name = name
    def add(self, root):
        pass 
    
#学校类
class School(root):
    childrenroot = []
    def add(self, root):
        self.childrenroot.append(root) 

#招生办
class StudentAdmissionDept(root):   
    def __init__(self, name):
        self.name = name

 
#师资部
class FacultyDepartment(root):    
    def __init__(self, name):
        self.name = name
 
  
root = School('总部')
root.add(StudentAdmissionDept('总部招生办'))
root.add(FacultyDepartment('总部师资部'))

s1 = School('桂林校区')
s1.add(StudentAdmissionDept('桂林校区 招生办'))
s1.add(FacultyDepartment('桂林校区 师资部'))
root.add(s1)

s2 = School('深圳校区')
s2.add(StudentAdmissionDept('深圳校区 招生办'))
s2.add(FacultyDepartment('深圳校区 师资部'))
root.add(s2)

s3 = School('广州校区')
s3.add(StudentAdmissionDept('广州校区 招生办'))
s3.add(FacultyDepartment('广州校区 师资部'))
root.add(s3)    

print('\n',root.name,'下级学校层级:\n')

for i in root.childrenroot:
    print(i.name)

Guess you like

Origin blog.csdn.net/A757291228/article/details/107040125