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的更多相关文章

  1. 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy

    Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...

  2. 【编程思想】【设计模式】【结构模式Structural】MVC

    Python版 https://github.com/faif/python-patterns/blob/master/structural/mvc.py #!/usr/bin/env python ...

  3. 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight

    Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...

  4. 【编程思想】【设计模式】【结构模式Structural】门面模式/外观模式Facade

    Python版 https://github.com/faif/python-patterns/blob/master/structural/facade.py #!/usr/bin/env pyth ...

  5. 【编程思想】【设计模式】【结构模式Structural】装饰模式decorator

    Python版 https://github.com/faif/python-patterns/blob/master/structural/decorator.py #!/usr/bin/env p ...

  6. 【编程思想】【设计模式】【结构模式Structural】组合模式composite

    Python版 https://github.com/faif/python-patterns/blob/master/structural/composite.py #!/usr/bin/env p ...

  7. 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge

    Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env pyth ...

  8. 【编程思想】【设计模式】【结构模式Structural】适配器模式adapter

    Python版 https://github.com/faif/python-patterns/blob/master/structural/adapter.py #!/usr/bin/env pyt ...

  9. 【编程思想】【设计模式】【结构模式Structural】3-tier

    Pyhon版 https://github.com/faif/python-patterns/blob/master/structural/3-tier.py #!/usr/bin/env pytho ...

随机推荐

  1. Java学到什么程度能找到一份还不错的工作

    我的读者里有很多 Java 新人,新人是指正在学 Java 的.以及工作时间不长的年轻人,他们经常问我一个问题: Java 学到什么程度才能找到一份还不错的工作? 今天我就从我自己面试新人的角度来回答 ...

  2. Vue.js教程 1.前端框架学习介绍

    Vue.js教程 1.前端框架学习介绍 什么是Vue.js 为什么要学习流行框架 什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站, ...

  3. Unicode、UTF-8、UTF-16 终于懂了

    计算机起源于美国,上个世纪,他们对英语字符与二进制位之间的关系做了统一规定,并制定了一套字符编码规则,这套编码规则被称为ASCII编码 ASCII 编码一共定义了128个字符的编码规则,用七位二进制表 ...

  4. liteIDE配置环境变量

    1.下载安装go https://www.cnblogs.com/Jack-cx/p/9878213.html 2.下载ide https://www.golangtc.com/download/li ...

  5. 旧电脑做服务器--第一篇 sql server 服务器搭建

    背景:旧电脑为2015年的老电脑,联系G50系列,目前键盘鼠标操作都有问题,键盘按键和鼠标左键莫名奇妙变成右击,屏幕显示也是大颗粒.但是配置还可以,16GB内存+256GB三星固态硬盘.所以想搭建作为 ...

  6. C++getline()

    #include <iostream>#include <cstring>#include <string>using namespace std;int main ...

  7. vivo统一告警平台设计与实践

    一.背景 一套监控系统检测和告警是密不可分的,检测用来发现异常,告警用来将问题信息发送给相应的人.vivo监控系统1.0时代各个监控系统分别维护一套计算.存储.检测.告警收敛逻辑,这种架构下对底层数据 ...

  8. [loj3304]作业题

    (以下假设$T=(V,\{e_{1},e_{2},...,e_{n-1} \})$是一棵树) 根据莫比乌斯反演,有$\gcd(w_{1},w_{2},...,w_{e_{n-1}})=\sum_{d| ...

  9. 多声部处理细节之crossstaff音符处理

    下面给出这几个案例的代码 ,可直接粘贴编译. \version "2.20.0" \language "english" \paper { #(set-pape ...

  10. c++基础知识02

    1.前置与后置区别 #include<iostream> using namespace std; int main() { //前置和后置区别 //前置递增或递减 先让变量加减1 然后进 ...