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