分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

设计模式, ?mvc 模型视图控制器模式(8)

发布时间:2023-09-06 01:26责任编辑:彭小芳关键词:暂无标签

MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。

  • Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
  • View(视图) - 视图代表模型包含的数据的可视化。
  • Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。
quotes = (‘A man is not complete until he is married. Then he is finished.‘, ?????????‘As I said before, I never repeat myself.‘, ?????????‘Behind a successful man is an exhausted woman.‘, ?????????‘Black holes really suck...‘, ‘Facts are stubborn things.‘)class QuoteModel: ???def get_quote(self, n): ???????try: ???????????value = quotes[n] ???????except IndexError as err: ???????????value = ‘Not found!‘ ???????return valueclass QuoteTerminalView: ???def show(self, quote): ???????print(‘And the quote is: "{}"‘.format(quote)) ???def error(self, msg): ???????print(‘Error: {}‘.format(msg)) ???def select_quote(self): ???????return input(‘Which quote number would you like to see?‘)class QuoteTerminalController: ???def __init__(self): ???????self.model = QuoteModel() ???????self.view = QuoteTerminalView() ???def run(self): ???????valid_input = False ???????while not valid_input: ???????????n = self.view.select_quote() ???????????try: ???????????????n = int(n) ???????????except ValueError as err: ???????????????self.view.error("Incorrect index ‘{}‘".format(n)) ???????????else: ???????????????valid_input = True ???????quote = self.model.get_quote(n) ???????self.view.show(quote)def main(): ???controller = QuoteTerminalController() ???while True: ???????controller.run()if __name__ == ‘__main__‘: ???main()

设计模式, ?mvc 模型视图控制器模式(8)

原文地址:https://www.cnblogs.com/ydf0509/p/8525949.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved