惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Vercel News
Vercel News
U
Unit 42
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
N
Netflix TechBlog - Medium
GbyAI
GbyAI
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
M
MIT News - Artificial intelligence
D
Docker
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog

博客园 - GeneJiang

窗口函数的使用 拉链表(Zipper Table)完整总结 —— 数据仓库 SCD Type 2 的经典实现 Kimball 维度建模:数据仓库领域最实用的“用户友好”方法论 DMP 数据接入测试全攻略:从“水龙头”把关到端到端信任 —— 理论解析 + 阿里云 DataWorks、AWS Glue、Azure Data Factory + 自研 EMR(基于 CDH)实战经验 ETL 全链路数据污染与逻辑错误定位实战经验分享 资产损失防控关键点-那些年被薅的“羊毛” 计算广告中常用的计算单位 Jupyter NoteBook 的快捷键使用指南 Hive常用函数 Hive Tutorial(一) - Hive的数据类型 基于TestNG和Maven的接口测试之(一)- 基础配置 195 Tenth line-取第十行 Python常用库之Requests自我总结 Linux基本命令(一) Presto Step by Step之一Presto简介 python 判断字符串是否包含子字符串 Python常用的排序 CentOS7安装(三)- 配置阿里云yum源 OSQA的配置 MySQL学习 (三) Limit-Distinct-Union
MVC设计模式(Python)
GeneJiang · 2020-08-12 · via 博客园 - GeneJiang

工作原理

MVC模式的工作机制为:

  1. 模型提供数据和业务逻辑(如何存储和查询信息),
  2. 视图负责数据的展示(如何呈现),
  3. 而控制器是两者之间的粘合剂,根据用户要求的呈现方式来协调模型和视图。

有趣的是,视图和控制器依赖于模型,而不是反过来。这主要是因为用户所关心的是数据。模型是可以独立工作的,这是MVC模式的关键所在。

MVC设计模式使用以下术语——模型、视图、控制器和客户端。

  • 模型:声明一个存储和操作数据的类。
  • 视图:声明一个类来构建用户界面和显示数据。
  • 控制器:声明一个连接模型和视图的类。
  • 客户端:声明一个类,根据某些操作来获得某些结果。

简而言之,MVC模式的主要意图如下。

  • 将数据和数据的展示隔离开来。
  • 使类的维护和实现更加简单。
  • 灵活地改变数据的存储和显示方式。

两者都是独立的,因此可以灵活修改。

应用

我们想要开发一个应用程序,告诉用户云公司所提供的营销服务,包括电子邮件、短信和语音设施。

我们首先要开发model类(模型),定义产品提供的服务,即电子邮件、短信和语音。这里的每种服务都有特定的费率,例如每1000封电子邮件将向客户收取2美元,而每1000条短信,费用为10美元,同时每1000条语音留言的费用为15美元。因此,模型提供与产品服务和价格相关的数据。

然后,我们来定义view类(视图),它提供了将信息反馈给客户端的方法。这些方法是list_services()list_ pricing(),从方法名称不难看出,一个是用于打印产品提供的服务,另一个是用来列出服务的定价。

接下来,我们开始定义Controller类,这个类定义了两个方法,即get_services()和get_pricing()。这两个方法都是用来查询模型并获取数据的,然后将数据馈送到视图,从而展示给用户。

class Model:
    services = {
        'email':{'number': 1000, 'price': 2}, 
        'sms':{'number':1000, 'price':10}, 
        'voice':{'number':1000, 'price': 20}, 
        'short-video':{'number':1000, 'price': 60}
    }

class View:
    def list_services(self, services):
        for svc in services:
            print(svc, ' ')

    
    def list_pricing(self, services):
        for svc in services:
            print("For {} {} message,  you may pay {}.".format(Model.services[svc]['number'], 
                                                         svc, 
                                                         Model.services[svc]['price']))

class Controller:
    def __init__(self):
        self.view = View()
        self.model = Model()
    
    def get_services(self):
        services = self.model.services.keys()
        return self.view.list_services(services)

    def get_pricing(self):
        services = self.model.services.keys()
        return self.view.list_pricing(services)

class Client:
    controller = Controller()
    print("Serices Proviced")
    controller.get_services()
    print("Prices")
    controller.get_pricing()

client = Client()        

Serices Proviced
email
sms
voice
short-video
Prices
For 1000 email message, you may pay 2.
For 1000 sms message, you may pay 10.
For 1000 voice message, you may pay 20.
For 1000 short-video message, you may pay 60.