分层的一点思考

#!/usr/bin/env python

import MySQLdb

print "Content-Type: text/html\n"
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"

connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")

for row in cursor.fetchall():
    print "<li>%s</li>" % row[0]

print "</ul>"
print "</body></html>"

connection.close()


如果不分层,UI做了一个页面,要想加进系统里面,累死。

但是如果把连接数据库和业务逻辑从里面解耦出来,那么开发人员就比较容易修改页面,最大化的利用UI的代码。

当然还有其他的优点

posted @ 2012-11-14 23:28  lein.wang  Views(145)  Comments(0Edit  收藏  举报