Advertisement
HyawehHoshikawa

Untitled

Nov 27th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. class Controller(object):
  2.     def __init__(self, task):
  3.         self.task = task        
  4.         self.value = 5
  5.  
  6.     def onChange(self, value):
  7.         self.value = value
  8.         self.task(value)
  9.  
  10. class View(object):
  11.     def __init__(self, data):
  12.         self.data = data
  13.         self.shown = self.data
  14.         self.controller = Controller(self.filterByColor) #
  15.         # ^ controllernya, di inisialisasi sama function yang perlu dikerjain controller
  16.  
  17.     def filterByColor(self, value):
  18.         self.shown = [x for x in self.data if x["color"] == value]
  19.         self.render()        
  20.  
  21.     def render(self):
  22.         # anggep aja, ngerender view ulang        
  23.         pass        
  24.  
  25. def main():            
  26.     data = [
  27.       {'color': 'red', 'student': 'budi'},
  28.       {'color': 'green', 'student': 'joko'},
  29.       {'color': 'blue', 'student': 'ani'},
  30.       {'color': 'green', 'student': 'hamdi'}
  31.     ]
  32.     view = View(data)
  33.     controller = view.controller        
  34.     print view.shown    
  35.     [{'color': 'red', 'student': 'budi'}, {'color': 'green', 'student': 'joko'}, {'color': 'blue', 'student': 'ani'}, {'color': 'green', 'student': 'hamdi'}]
  36.  
  37.     controller.onChange(value="green")    
  38.     print view.shown
  39.     [{'color': 'green', 'student': 'joko'}, {'color': 'green', 'student': 'hamdi'}]
  40.    
  41. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement