Skip to content

Commit 85bda75

Browse files
committed
added front controller design pattern.
1 parent 07fe6ec commit 85bda75

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Current Patterns:
2525
| [decorator](decorator.py) | wrap functionality with other functionality in order to affect outputs |
2626
| [facade](facade.py) | use one class as an API to a number of others |
2727
| [factory_method](factory_method.py) | delegate a specialized function/method to create instances |
28+
| [front controller](front_controller.py) | single handler requests coming to the application |
2829
| [flyweight](flyweight.py) | transparently reuse existing instances of objects with similar/identical state |
2930
| [graph_search](graph_search.py) | (graphing algorithms, not design patterns) |
3031
| [lazy_evaluation](lazy_evaluation.py) | lazily-evaluated property pattern in Python |

front_controller.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
@author: Gordeev Andrei <[email protected]>
6+
The controller provides a centralized entry point that controls and manages
7+
request handling.
8+
"""
9+
10+
11+
class MobileView(object):
12+
def show_index_page(self):
13+
print('Displaying mobile index page')
14+
15+
16+
class TabletView(object):
17+
def show_index_page(self):
18+
print('Displaying tablet index page')
19+
20+
21+
class Dispatcher(object):
22+
def __init__(self):
23+
self.mobile_view = MobileView()
24+
self.tablet_view = TabletView()
25+
26+
def dispatch(self, request):
27+
if request.type == Request.mobile_type:
28+
self.mobile_view.show_index_page()
29+
elif request.type == Request.tablet_type:
30+
self.tablet_view.show_index_page()
31+
else:
32+
print('cant dispatch the request')
33+
34+
35+
class RequestController(object):
36+
""" front controller """
37+
def __init__(self):
38+
self.dispatcher = Dispatcher()
39+
40+
def dispatch_request(self, request):
41+
if isinstance(request, Request):
42+
self.dispatcher.dispatch(request)
43+
else:
44+
print('request must be a Request object')
45+
46+
47+
class Request(object):
48+
""" request """
49+
50+
mobile_type = 'mobile'
51+
tablet_type = 'tablet'
52+
53+
def __init__(self, request):
54+
self.type = None
55+
request = request.lower()
56+
if request == self.mobile_type:
57+
self.type = self.mobile_type
58+
elif request == self.tablet_type:
59+
self.type = self.tablet_type
60+
61+
62+
if __name__ == '__main__':
63+
front_controller = RequestController()
64+
front_controller.dispatch_request(Request('mobile'))
65+
front_controller.dispatch_request(Request('tablet'))
66+
67+
front_controller.dispatch_request(Request('desktop'))
68+
front_controller.dispatch_request('mobile')
69+
70+
71+
### OUTPUT ###
72+
# Displaying mobile index page
73+
# Displaying tablet index page
74+
# cant dispatch the request
75+
# request must be a Request object

0 commit comments

Comments
 (0)