Python设计模式之MVC模式简单示例

yipeiwu_com6年前Python基础

本文实例讲述了Python设计模式之MVC模式。分享给大家供大家参考,具体如下:

一.简单介绍

mvc模式  the  model-view-controller pattern

mvc模式是一个运用在软件工程中的设计模式。mvc模式脱离了以前简单的web服务设计逻辑,将开发,测试和维护分离。在MVC模式中,应用被分解为相互作用的模块,模型,视图,控制。目的在于分离输入(control),处理逻辑(model),输出格式(view)。

简单的理解:

1. 控制模块用于获取用户输入,并将模型与视图建立联系
2. 模型主要是从存储区获取数据
3. 视图用于展示给用户,从模型获取的数据

具体细节:

控制模块:可以被看作是一个介于用户,处理(model),显示(view)之间的中间人。它是用户请求的入口,也是应用处理的入口。控制模块接受用户输入,解析,决定哪一个model和view参与处理,因此,它决定了针对用户请求,选择何种view和model。

模型模块:处理业务的应用程序,model操作数据库,比如插入,更新,删除。每个模型会提供固定类型的数据给控制模块,另一方面,控制模块可以调用模型的不同方法处理数据,并将处理后的结果返回给视图模型

视图模块:主要用来显示,通过控制模块获取模型模块处理后的数据,并进行格式化的显示。通过控制模块选择view并显示反馈给用户。view模型的选择是基于模型模块的l选择和用户配置等等。

二.简单的例子

测试管理系统用来查询错误列表

情景描述:

如果用户查询一个特定的错误,测试管理系统以某种格式显示这个错误的描述
如果用户搜索相关错误的关键值,测试管理系统显示所有相关的错误列表

创建SQLite 数据库,库名TMS,并创建一个表

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')

总结:通过此mvc设计方法,看到了解耦的好处,个个模块独立,相互不影响,也可以增加模块。方便组合,方便拆卸。好好体会吧!

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

tensorflow中next_batch的具体使用

本文介绍了tensorflow中next_batch的具体使用,分享给大家,具体如下: 此处给出了几种不同的next_batch方法,该文章只是做出代码片段的解释,以备以后查看:...

Windows下的Jupyter Notebook 安装与自定义启动(图文详解)

Windows下的Jupyter Notebook 安装与自定义启动(图文详解)

【听图阁-专注于Python设计】小编注:如果不是特殊需要建议安装 Anaconda3 即可,自带Jupyter Notebook 。 手动安装之前建议查看这篇文章:/post/1351...

Python win32com 操作Exce的l简单方法(必看)

实例如下: from win32com.client import Dispatch import win32com.client class easyExcel:...

Python和php通信乱码问题解决方法

即使在urlencode之前str.decode(“cp936″).encode(“utf-8″)做了编码转换也是没用的。后来查询手册查到一个urllib.quote()函数,用此方法成...

python版DDOS攻击脚本

本文实例为大家分享了python版DDOS攻击脚本,供大家参考,具体内容如下 于是就找到了我之前收藏的一篇python的文章,是关于ddos攻击的一个脚本,正好今天有空,就实践下了。 附...