The combination mode is to group a category into a whole, and organize the relationship between multiple wholes, using a tree structure to describe all the wholes.
The general writing method 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:
# root
classroot:
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:
# School class
classSchool(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:
# Admissions Office
classStudentAdmissionDept(root):
def __init__(self, name):
self.name = name
# Faculty Department
classFacultyDepartment(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('Headquarters')
root.add(StudentAdmissionDept('Headquarters Admissions Office'))
root.add(FacultyDepartment('Headquarters Faculty Department'))
After creating a 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('Guilin Campus')
s1.add(StudentAdmissionDept('Guilin Campus Admissions Office'))
s1.add(FacultyDepartment('Guilin Campus Teachers Department'))
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('Shenzhen campus')
s2.add(StudentAdmissionDept('Shenzhen Campus Admissions Office'))
s2.add(FacultyDepartment('Teachers Department of Shenzhen Campus'))
root.add(s2)
s3 =School('Guangzhou campus')
s3.add(StudentAdmissionDept('Guangzhou Campus Admissions Office'))
s3.add(FacultyDepartment('Teachers Department of Guangzhou Campus'))
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,'Lower school level:\n')for i in root.childrenroot:print(i.name)
The result is:
Since 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:
# root
classroot:
name =''
def __init__(self, name):
self.name = name
def add(self, root):
pass
# School class
classSchool(root):
childrenroot =[]
def add(self, root):
self.childrenroot.append(root)
# Admissions Office
classStudentAdmissionDept(root):
def __init__(self, name):
self.name = name
# Faculty Department
classFacultyDepartment(root):
def __init__(self, name):
self.name = name
root =School('Headquarters')
root.add(StudentAdmissionDept('Headquarters Admissions Office'))
root.add(FacultyDepartment('Headquarters Faculty Department'))
s1 =School('Guilin Campus')
s1.add(StudentAdmissionDept('Guilin Campus Admissions Office'))
s1.add(FacultyDepartment('Guilin Campus Teachers Department'))
root.add(s1)
s2 =School('Shenzhen campus')
s2.add(StudentAdmissionDept('Shenzhen Campus Admissions Office'))
s2.add(FacultyDepartment('Teachers Department of Shenzhen Campus'))
root.add(s2)
s3 =School('Guangzhou campus')
s3.add(StudentAdmissionDept('Guangzhou Campus Admissions Office'))
s3.add(FacultyDepartment('Teachers Department of Guangzhou Campus'))
root.add(s3)print('\n',root.name,'Lower school level:\n')for i in root.childrenroot:print(i.name)
The above is the detailed content of python3's simple implementation of the combination design pattern. For more information about the implementation of the combination design pattern in Python, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts