【编程思想】【设计模式】【结构模式Structural】front_controller
Python版
https://github.com/faif/python-patterns/blob/master/structural/front_controller.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
@author: Gordeev Andrey <gordeev.and.and@gmail.com> *TL;DR80
Provides a centralized entry point that controls and manages request handling.
""" class MobileView(object): def show_index_page(self):
print('Displaying mobile index page') class TabletView(object): def show_index_page(self):
print('Displaying tablet index page') class Dispatcher(object): def __init__(self):
self.mobile_view = MobileView()
self.tablet_view = TabletView() def dispatch(self, request):
if request.type == Request.mobile_type:
self.mobile_view.show_index_page()
elif request.type == Request.tablet_type:
self.tablet_view.show_index_page()
else:
print('cant dispatch the request') class RequestController(object):
""" front controller """ def __init__(self):
self.dispatcher = Dispatcher() def dispatch_request(self, request):
if isinstance(request, Request):
self.dispatcher.dispatch(request)
else:
print('request must be a Request object') class Request(object):
""" request """ mobile_type = 'mobile'
tablet_type = 'tablet' def __init__(self, request):
self.type = None
request = request.lower()
if request == self.mobile_type:
self.type = self.mobile_type
elif request == self.tablet_type:
self.type = self.tablet_type if __name__ == '__main__':
front_controller = RequestController()
front_controller.dispatch_request(Request('mobile'))
front_controller.dispatch_request(Request('tablet')) front_controller.dispatch_request(Request('desktop'))
front_controller.dispatch_request('mobile') ### OUTPUT ###
# Displaying mobile index page
# Displaying tablet index page
# cant dispatch the request
# request must be a Request object
Python转载版
【编程思想】【设计模式】【结构模式Structural】front_controller的更多相关文章
- 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...
- 【编程思想】【设计模式】【结构模式Structural】MVC
Python版 https://github.com/faif/python-patterns/blob/master/structural/mvc.py #!/usr/bin/env python ...
- 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight
Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【结构模式Structural】门面模式/外观模式Facade
Python版 https://github.com/faif/python-patterns/blob/master/structural/facade.py #!/usr/bin/env pyth ...
- 【编程思想】【设计模式】【结构模式Structural】装饰模式decorator
Python版 https://github.com/faif/python-patterns/blob/master/structural/decorator.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【结构模式Structural】组合模式composite
Python版 https://github.com/faif/python-patterns/blob/master/structural/composite.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge
Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env pyth ...
- 【编程思想】【设计模式】【结构模式Structural】适配器模式adapter
Python版 https://github.com/faif/python-patterns/blob/master/structural/adapter.py #!/usr/bin/env pyt ...
- 【编程思想】【设计模式】【结构模式Structural】3-tier
Pyhon版 https://github.com/faif/python-patterns/blob/master/structural/3-tier.py #!/usr/bin/env pytho ...
随机推荐
- 【Python+postman接口自动化测试】(8)以青云客机聊天器人和图灵聊天机器人接口示范python发送get和post
以青云客机器人和图灵机器人接口示范python发送get和post 发送请求,我们这里主要使用Python的一个第三方包(需要先安装):requests. Python3自带的http.client和 ...
- Redis核心原理与实践--事务实践与源码分析
Redis支持事务机制,但Redis的事务机制与传统关系型数据库的事务机制并不相同. Redis事务的本质是一组命令的集合(命令队列).事务可以一次执行多个命令,并提供以下保证: (1)事务中的所有命 ...
- 12组-Alpha冲刺-3/6
一.基本情况 队名:字节不跳动 组长博客:https://www.cnblogs.com/147258369k/p/15546442.html 小组人数:10人 二.冲刺概况汇报 侯钦凯 过去两天完成 ...
- centos7系列的网络yum源配置
因为新安装centos机器yum比较旧,主要是对网易源进行配置,其它源也差不多.我是在securecrt远程ssh工具操作的,非虚拟机软件上. yum install lszrz -y 安装上传工 ...
- 仿Word的支持横轴竖轴的WPF 标尺
最近在 https://mp.weixin.qq.com/s/3dEO0NZQv5YLqK72atG4Wg 官方公众号看到了 用WPF 制作 标尺 在去年项目上也接到了一个需求,用于排版自定义拖 ...
- [loj3364]植物比较
结论:设$b_{i}$满足该限制,则$a_{i}$合法当且仅当$\forall i\ne j,a_{i}\ne a_{j}$且$\forall |i-j|<k,[a_{i}<a_{j}]= ...
- [noi1755]Trie
定义S对应的数组为$a_{i}=\min_{0\le j<i,S_{j}=S_{i}}i-j$,特别的,若不存在j,令$a_{i}=i$,那么容易发现存在双射关系就意味这两者对应的数组相同 因此 ...
- 洛谷 P4094 [HEOI2016/TJOI2016]字符串(SA+主席树)
题面传送门 一道码农题---- u1s1 感觉这类题目都挺套路的,就挑个有代表性的题写一篇题解罢. 首先注意到答案满足可二分性,故考虑二分答案 \(mid\),转化为判定性问题. 考虑怎样检验 \(m ...
- Docker-Mysql-proxy Mysql Proxy实现读写分离
Docker-Mysql-proxy Mysql实现读写分离与负载 原理 MySQL Proxy处于客户端应用程序和MySQL服务器之间,通过截断.改变并转发客户端和后端数据库之间的通信来实现其功 ...
- illumina SNP 芯片转基因型矩阵
一.芯片数据 此次拿到的illumina芯片数据并不是原始的数据,已经经过GenomeStudio软件处理成了finalreport文件,格式如下: 之前没处理过芯片数据,对于这种编码模式(Forwa ...