Python設(shè)計模式之MVC模式簡單示例
本文實例講述了Python設(shè)計模式之MVC模式。分享給大家供大家參考,具體如下:
一.簡單介紹
mvc模式 the model-view-controller pattern
mvc模式是一個運用在軟件工程中的設(shè)計模式。mvc模式脫離了以前簡單的web服務(wù)設(shè)計邏輯,將開發(fā),測試和維護分離。在MVC模式中,應(yīng)用被分解為相互作用的模塊,模型,視圖,控制。目的在于分離輸入(control),處理邏輯(model),輸出格式(view)。
簡單的理解:
1. 控制模塊用于獲取用戶輸入,并將模型與視圖建立聯(lián)系
2. 模型主要是從存儲區(qū)獲取數(shù)據(jù)
3. 視圖用于展示給用戶,從模型獲取的數(shù)據(jù)

具體細節(jié):
控制模塊:可以被看作是一個介于用戶,處理(model),顯示(view)之間的中間人。它是用戶請求的入口,也是應(yīng)用處理的入口??刂颇K接受用戶輸入,解析,決定哪一個model和view參與處理,因此,它決定了針對用戶請求,選擇何種view和model。
模型模塊:處理業(yè)務(wù)的應(yīng)用程序,model操作數(shù)據(jù)庫,比如插入,更新,刪除。每個模型會提供固定類型的數(shù)據(jù)給控制模塊,另一方面,控制模塊可以調(diào)用模型的不同方法處理數(shù)據(jù),并將處理后的結(jié)果返回給視圖模型
視圖模塊:主要用來顯示,通過控制模塊獲取模型模塊處理后的數(shù)據(jù),并進行格式化的顯示。通過控制模塊選擇view并顯示反饋給用戶。view模型的選擇是基于模型模塊的l選擇和用戶配置等等。
二.簡單的例子
測試管理系統(tǒng)用來查詢錯誤列表
情景描述:
如果用戶查詢一個特定的錯誤,測試管理系統(tǒng)以某種格式顯示這個錯誤的描述
如果用戶搜索相關(guān)錯誤的關(guān)鍵值,測試管理系統(tǒng)顯示所有相關(guān)的錯誤列表
創(chuàng)建SQLite 數(shù)據(jù)庫,庫名TMS,并創(chuàng)建一個表
| ID | Component | Summary |
| 1 | XYZ | File doesn't get deleted |
| 2 | XYZ | Registry doesn't get created |
| 3 | ABC | Wrong title gets displayed |
代碼如下:
#mvc.py
import sqlite4
import types
class DefectModel:
def getDefectList(self, component):
query = "select ID from defects where Component= '%s' " % component
defectlist = self._dbselect(query)
list = []
for row in defectlist:
list.append(row[0])
return list
def getSummary(self, id):
query = "select summary from defects where ID='%d'" % id
summary = self._dbselect(query)
for row in summary:
return row[0]
def _dbselect(self, query):
connection = sqlite3.connect('TMS')
cursorObj = connection.cursor()
results = cursorObj.execute(query)
connection.commit()
cursorObj.close()
return results
class DefectView:
def summary(self, summary, defectid):
print "#### Defect Summary for defect# %d####%s\n" %(defectid, summary)
def defectList(self, list, category):
print "#### Defect List for %s ####\n" % category
for defect in list:
print defect
class Controller:
def __init__(self):
pass
def getDefectSummary(self, defectid):
model = DefectModel()
view = DefectView()
summary_data = model.getSummary(defectid)
return view.summary(summary_data, defectid)
def getDefectList(self, component):
model = DefectModel()
view = DefectView()
defectlist_data = model.getDefectList(component)
return view.defectList(defectlist_data, component)
使用模塊:
import mvc
controller = mvc.Controller()
print controller.getDefectSummary(2)
print controller.getDefectList('ABC')
總結(jié):通過此mvc設(shè)計方法,看到了解耦的好處,個個模塊獨立,相互不影響,也可以增加模塊。方便組合,方便拆卸。好好體會吧!
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python腳本實現(xiàn)抓取指定網(wǎng)站上的所有圖片
對于開發(fā)者、數(shù)據(jù)分析師以及研究人員而言,從網(wǎng)頁中提取有價值的信息是一項至關(guān)重要的技能,本文將詳細介紹如何使用Python編寫一個腳本來自動抓取指定網(wǎng)站上的所有圖片,需要的可以參考下2024-10-10
Python?PyWebIO開發(fā)Web應(yīng)用實例探究
這篇文章主要為大家介紹了Python?PyWebIO開發(fā)Web應(yīng)用實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
在Python中使用Neo4j數(shù)據(jù)庫的教程
這篇文章主要介紹了在Python中使用Neo4j數(shù)據(jù)庫的教程,Neo4j是一個具有一定人氣的非關(guān)系型的數(shù)據(jù)庫,需要的朋友可以參考下2015-04-04
Python實現(xiàn)刪除Android工程中的冗余字符串
這篇文章主要介紹了Python實現(xiàn)刪除Android工程中的冗余字符串,本文實現(xiàn)的是刪除Android資源(語言)國際化機制中的一些冗余字符串,需要的朋友可以參考下2015-01-01

