【编程思想】【设计模式】【结构模式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 ...
随机推荐
- Vue的第一课
终于学习到Vue了,美滋滋,给自己点个赞 前后端作用: 1.1vs1(一个Vue对象控制一个) <body> <div id="app"> <p> ...
- 直播预告 | 猪齿鱼V1.1发布,线上新功能详解邀您参加
2021年11月11日,数智化效能平台猪齿鱼 Choerodon发布 V1.1版本,多项功能新增或优化,多管齐下,全面提升团队工作效能! 通过提供体系化方法论和协作.测试.DevOps及容器工具,猪齿 ...
- vue + cesium开发(5) 搭建 vue + cesium开发环境(2)
上vue+cesium开发(1)中,没有进行配置webpack,而是使用了插件进行代替,在使用过程中出现了一些未知BUG,影响体验,因此参考了官方文档对项目进行重新配置,使用了 copy-webpac ...
- 网络带宽和速度测试windows和linux用iperf工具
网络带宽和速度测试windows和linux用iperf工具 Iperf是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性 ...
- [hdu6326]Monster Hunter
考虑树是以1为中心的菊花图的情况,也即如何安排打怪兽的顺序 用二元组$(a,b)$来描述怪兽,则对于两个怪兽$(a_{1},b_{1})$和$(a_{2},b_{2})$,交换两者不会影响血量的变化量 ...
- [bzoj5462]新家
先离线,将询问按照时间排序,维护商店出现和消失 对于每一个询问,先二分枚举答案,假设是ans, 即要求对于区间[l-ans,l+ans],商店的种类数是k(l是询问的位置) (当然需要先将所有位置离散 ...
- MyBatis-Plus中如何使用ResultMap
MyBatis-Plus (简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发.提高效率而生. MyBatis-Plus对MyBatis基本零侵入,完全可以 ...
- BombEnemy(炸弹人)
// // Created by Administrator on 2021/7/25. // #ifndef C__TEST01_BOMBENEMY_HPP #define C__TEST01_BO ...
- [NOIP2018 提高组] 旅行
考虑如果我们要回溯的话,一定要把非环上的子树都搜索完. 而在环上的一个地方回溯,相当于把环上的下一个点置于所有环的顺序的最后. 所以我们只有在环上遇到环上的最大点时且周围的点都比这个点小时非正常回溯即 ...
- Codeforces 1392I - Kevin and Grid(平面图的欧拉定理+FFT)
Codeforces 题面传送门 & 洛谷题面传送门 模拟赛考到一道和这题有点类似的题就来补了 神仙 GLBR I %%%%%%%%%%%%%%%%%%%% 不过感觉见过类似的题目之后就比较套 ...