-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModelViewControllerPattern.py
More file actions
122 lines (89 loc) · 3.04 KB
/
Copy pathModelViewControllerPattern.py
File metadata and controls
122 lines (89 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Model View Controller Pattern - A compound pattern consisting of Observer, Strategy
and Composite Patterns. It uses Observer Pattern to keep observers updated yet
stay decoupled. The controller is the strategy pattern for the view. View gets
different behaviour from the controller. The view uses composite pattern to update
its view components.
Model - Application logic sits here and it provides data and states to the view.
Controller - No application logic should sit here, t should only implement behaviour
for the view and translate action to and fro. It sends these actions to the model
and the model will decide what to do based on its application logic.
View - It is the display to present the data.
Example - An MP3 Player is a good example that uses the MVC pattern, the view is
the interface. When buttons are pressed, the controller will be activated and will
call the model to manipulate data, state and then update the view accordingly.
"""
class Song:
def __init__(self, song):
self.song = song
self.status = ''
def play(self):
self.status = 'Playing: %s' % (self.song)
def stop(self):
self.status = 'Song stopped, last song played: %s' % (self.song)
self.song = ''
def getStatus(self):
return self.status
class PlayerModel:
def __init__(self, volume):
self.volume = volume
self.song = ''
self.viewObservers = []
def setVolume(self, volume):
self.volume = volume
self.updateViews()
def getVolume(self):
return self.volume
def setSong(self, song):
self.song = song
self.updateViews()
def playSong(self):
self.song.play()
self.updateViews()
def stopSong(self):
self.song.stop()
self.updateViews()
def registerViewObserver(self, view):
self.viewObservers.append(view)
def removeViewObserver(self, view):
self.viewObservers.remove(view)
def updateViews(self):
for view in self.viewObservers:
view.update(self)
def toString(self):
print 'Song Status: %s\n Volume: %d' % (self.song.getStatus(), self.getVolume())
class PlayerController:
def __init__(self, model, view):
self.model = model
self.view = view
def openFile(self):
file = self.view.popupFileDialog()
self.model.setSong(file)
def playButtonPressed(self):
self.model.playSong()
def stopButtonPressed(self):
self.model.stopSong()
def volumeSliderChanged(self):
# use fake data as there are no view components for this
volume = 80
self.model.setVolume(volume)
class PlayerView:
def __init__(self):
self.song = ''
def update(self, model):
model.toString()
def popupFileDialog(self):
return self.song
def uploadSong(self, song):
self.song = song
song1 = Song('Imagine - John Lennon')
song2 = Song('It\'s my life - Bon Jovi')
jukeboxDisplay = PlayerView()
jukeboxModel = PlayerModel(50)
jukeboxModel.registerViewObserver(jukeboxDisplay)
jukeboxController = PlayerController(jukeboxModel, jukeboxDisplay)
jukeboxDisplay.uploadSong(song1)
jukeboxController.openFile()
jukeboxController.playButtonPressed()
jukeboxController.stopButtonPressed()
jukeboxController.volumeSliderChanged()