Model View Controller. Prasun Dewan Comp 114. Model View Controller Pattern. Issue How to create user-interface objects like object editor Model-View-Controller Pattern Observer sub-pattern. User Interface Objects. How to reuse code among different user interfaces?
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
Model View Controller Pattern • Issue • How to create user-interface objects like object editor • Model-View-Controller Pattern • Observer sub-pattern
User Interface Objects • How to reuse code among different user interfaces? • How to simultaneously create multiple user interfaces to same object? • Different views for same user. • Slide sorter vs. Outline • How to create same or different view for each user sharing an object • Distributed presentation
Counter • Can add arbitrary positive/negative value to an integer. • Different user interfaces.
Observable 2 Notification Scheme • Each observer is registered with observable. • Each write method in observable calls a notification method in each observer. • Notification method in observer reads model . Observer 1 Observer 2 Observable 1 Observer 3 • Each student is registered with professor’s listserv. • When web page is updated mail sent to students. • Student reads web page. Observer N
Observable 2 General Notification Scheme • Observers may have multiple observerables with common notification method. • Notification method parameter indicates which observable. Observer 1 Observer 2 Observable 1 Observer 3 Observer N
Implementation dependent issues • How does controller know about model? • Model connection method invoked on it. • By model or some other program • Main • How is observable registered with observer. • It registers itself if it knows about observable. • Model registers it if it knows about observer. • Some other code registers it • Main
Performs Input Performs Output Model connection method Observer registration method Notification method Write method Read method Model, View and Controller (MVC) View Controller Model
Console Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new ACounterConsoleView()); CounterController controller = new ACounterController(); controller.setModel(model); controller.processInput(); }
Input Code Shared Console and JOption Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new ACounterJOptionView()); CounterController controller = new ACounterController(); controller.setModel(model); controller.processInput(); }
Console Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new ACounterConsoleView()); CounterController controller = new ACounterController(); controller.setModel(model); controller.processInput(); }
Composition code duplicated Mixed UI Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver(new AConsoleJOptionView()); model.addObserver (new ACounterConsoleView()); CounterController controller = new ACounterController(); controller.setModel(model); controller.processInput(); }
Façade: Compose objects into a single unit exposing methods relevant to that unit • E.g. scanner, parser, program tree, code generator objects combined into one compiler object • Takes program text as input • Produces code as output • Passes text to scanner, which passes tokens to parser, which creates program tree, which is processed by code generator, which produces output • Compiler user not aware of internal components • Componentizing code is a pain as components must be combined • Facades removes this problem, creating a simple façade to complex internals
Interactor Facade • Provides a single edit() method taking model as argument • Instantiates controller and view. • Makes view observer of model • Connects controller to model. • Starts controller.
Must be last action ConsoleControllerAndView Facade package interactors; import models.ObservableCounter; import models.CounterObserver; import controllers.ACounterController; import controllers.CounterController; import views.ACounterConsoleView; publicclass AConsoleControllerAndView implements CounterInteractor { publicvoid edit(ObservableCounter model) { CounterObserver view = new ACounterConsoleView(); model.addObserver(view); CounterController controller = new ACounterController(); controller.setModel(model); controller.processInput(); } }
Console Controller And JOption View Main package main; import models.AnObservableCounter; import facades.AConsoleControllerAndJOptionView; publicclass ACounterMain { publicstatic void main(String[] args) { (new AConsoleContollerAndJOptionView()).edit(new AnObservableCounter()); } }