python three-tier architecture description

Keywords: Python SQL

Three-tier architecture (3-tier architecture) generally means that the whole business application is divided into:
Presentation layer, Business Logic Layer and Data Access layer.


The purpose of differentiating levels is for the idea of "high cohesion and low coupling".
High cohesion and low coupling are the concepts of software engineering and the criteria for judging the design quality. They are mainly object-oriented design, which mainly depends on whether the class cohesion is high and the coupling degree is low.
Cohesion is the degree of tightness of each element in a module, and high cohesion is the degree of tightness of each element in a module.


The so-called high cohesion refers to a software module is composed of highly relevant code, only responsible for one task, that is, the commonly used single responsibility principle.
Coupling: A measure of the degree of interconnection between different modules in a software architecture (coupling is also called inter-block linkage). A measure of the degree of interconnection between modules in a software system structure.
The closer the connection between modules, the stronger the coupling and the worse the independence of modules. The coupling between modules depends on the complexity of the interface between modules, the way of invocation and the information transmitted. )
For low coupling, the superficial understanding is:
A complete system, between modules and modules, exists as independently as possible.
That is to say, let each module complete a specific sub-function as independently as possible.
The interface between module and module is as few and simple as possible.
If the relationship between two modules is complex, it is better to consider further module partitioning first.
This is conducive to modification and composition.

Three-tier architecture, as follows:


 

1. Presentation layer (UI): Popularly speaking, it is the interface presented to the user, that is, what the user sees when he uses a system.
2. Business Logic Layer (BLL): Operations for specific problems, or data layer operations, data business logic processing.
3. Data Access Layer (DAL): Direct manipulation of what the Layer does data base For data addition, deletion, modification, search and so on.

 

Example:

 

 1 #coding:utf8
 2 
 3 from utility.sql_helper import MySqlHelper
 4 
 5 class Admin(object):
 6     def __init__(self):
 7         self.__helper = MySqlHelper()
 8         
 9     def Get_One(self,id):
10         sql = "select * from userinfo where id = %s"
11         params = (id,)
12         return self.__helper.Get_One(sql, params)
13     
14     def CheckValidate(self,username,password):
15         sql = "select * from userinfo where name=%s and password=%s"
16         params = (username,password,)
17         return self.__helper.Get_One(sql, params)
18     
19     
admin.py
 1 #coding:utf8
 2 import MySQLdb
 3 import conf
 4 
 5 
 6 class MySqlHelper(object):
 7     def __init__(self):
 8         self.__conn_dict = conf.conn_dict
 9     def Get_Dict(self,sql,params):
10         conn = MySQLdb.connect(**self.__conn_dict)
11         cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) 
12         
13         reCount = cur.execute(sql,params)
14         data = cur.fetchall()   
15 
16         cur.close()
17         conn.close()
18         return data
19        
20     def Get_One(self,sql,params):
21         conn = MySQLdb.connect(**self.__conn_dict)
22         cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) 
23         
24         reCount = cur.execute(sql,params)
25         data = cur.fetchone()  
26 
27         cur.close()
28         conn.close()
29         return data
sql_helper

 

1 #coding:utf8
2 
3 conn_dict = dict(
4     host='127.0.0.1',
5     user='root',
6     passwd='123456',
7     db ='Admin'
8     )
conf.py
 1 #coding:utf8
 2 
 3 from module.admin import Admin
 4 
 5 def main():
 6     user=raw_input('username:')
 7     pwd=raw_input("password:")
 8     admin = Admin()
 9     result = admin.CheckValidate(user, pwd)
10     if not result:
11         print 'ERROR Incorrect username or password'
12     else:
13         print 'Enter the background login interface'
14 
15 if __name__== '__main__':
16     main()
index.py

Posted by coppercoins on Mon, 15 Apr 2019 23:09:33 -0700