zoukankan      html  css  js  c++  java
  • Python设计模式之MVC模式

    # -*- coding: utf-8 -*-
    # author:baoshan
    
    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 subborn things.')
    
    
    class QuoteMode:
        def get_quote(self, n):
            try:
                value = quotes(n)
            except IndexError as err:
                value = 'Not found!'
            return value
    
    
    class 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.mode = QuoteMode()
            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.mode.get_quote(n)
            self.view.show(quote)
    
    
    def main():
        controller = QuoteTerminalController()
        while True:
            controller.run()
    
    if __name__ == '__main__':
        main()

    代码展示,如何设计MVC模式

    参考自:精通Python设计模式 之 MVC模式

  • 相关阅读:
    C#生成PDF总结
    Oracle删除当前用户下所有的表的方法
    C#操作oracle 到ExecuteNonQuery卡死不执行
    C#中事件的使用
    初探three.js光源
    d3.js 地铁轨道交通项目实战
    初探three.js
    d3.js 绘制北京市地铁线路状况图(部分)
    d3.js 共享交换平台demo
    d3.js 实现烟花鲜果
  • 原文地址:https://www.cnblogs.com/zhzhang/p/11338595.html
Copyright © 2011-2022 走看看