Python three-tier architecture description, python three-tier architecture
A three-tier architecture usually divides the entire business application:
Presentation layer, Business Logic Layer, and Data access layer ).
The purpose of differentiation is to "High Cohesion and low coupling.
High Cohesion and low coupling are the concepts in software engineering, the criteria for judging the design quality, mainly object-oriented design, mainly to see whether the class cohesion is high, and whether the coupling degree is low.
Cohesion is the closeness of elements in a module. High Cohesion is the closeness of elements in a module.
High cohesion refers to a software module that consists of highly correlated Code and is responsible for only one task, that is, a single responsibility principle.
Coupling: a measurement of the degree of interconnection between different modules in a software structure (coupling is also called inter-block connection. A measure of the closeness between modules in the software system structure.
The closer the relationship between modules, the stronger the coupling, and the poorer the module independence. The coupling between modules depends on the complexity of Inter-module interfaces, the call methods, and the transmitted information .)
For low coupling, the rough understanding is:
A complete system, between modules, try to make it independent.
That is to say, let each module complete a specific sub-function as independently as possible.
The interfaces between modules are as few as possible and as simple as possible.
If the relationship between two modules is complex, it is best to consider further module division first.
This facilitates modification and combination.
Three-tier architecture, such:
1. Presentation layer (UI): The interface displayed to the user, that is, what the user sees when using a system.
2. Business logic layer (BLL): operations for specific problems can also be said to be operations on the data layer and data business logic processing.
3. Data access layer (DAL): transactions made at this layer operate the database directly, and add, delete, modify, and search data.
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 data19 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:utf82 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 'user name or Password error '12 else: 13 print 'Enter the background logon interface '14 15 if _ name __= '_ main _': 16 main ()
Index. py