Pattern: Model-View-Controller (MVC) Beat Generator MVC is an architectural pattern it specifies code organization at a higher level than a design pattern. (But the idea is similar it tells you how to organize things.) controller has methods for the conceptual operations There are many related patterns (e.g. MVP) and varying interpretations, but The key idea is separation of concerns separating distinct functionality within an application's code to allow reuse of code across applications development of multiple UIs developer specialization and focus parallel development by separate teams view provides the UI model contains the application state, and manages/ notifies observers CPSC 329: Software Development Fall 2017 124 CPSC 329: Software Development Fall 2017 126 Model-View-Controller MVC divides an application's code into three parts: the model stores data objects and business rules data objects are the basic noun objects needed by the system business rules govern the manipulation of the model e.g. define what updates are allowed the view is what the user sees (user interface) displays the data translates user interactions into conceptual actions the controller is the glue in between contains the application logic translates user actions into model updates selects the view Model-View-Controller 1. the user does something to the view e.g. clicks a button 2. do this task 3. modify interface (*) 4. I've changed! 5. Tell me your state! (**) (*) changes to the components shown, whether they are enabled/disabled, etc not simply refreshing a component to show new model data (**) in pull model 3. change state CPSC 329: Software Development Fall 2017 125 CPSC 329: Software Development Fall 2017 127
Model-View-Controller The pattern is derived from familiar concepts: single purpose separate view from the rest loose coupling, especially with respect to the model model does not know about view or controllers information hiding / code to the interface to isolate change and allow substitution of parts / reuse each view can consist of multiple classes model can consist of multiple classes can have different controllers for different views (e.g. views have different functionality) controllers can also implement Observer if they need to be notified of model updates CPSC 329: Software Development Fall 2017 129 CPSC 329: Software Development Fall 2017 131 Basic MVC Observer pattern allows model to be decoupled from view (model can notify view of updates without having to know about view) can utilize Strategy pattern for views so one controller can work with different views (e.g. similar functionality in views but different realizations) beat example lacked an explicit Observable instead it combined model and observable in BeatGenerator (a reason two kinds of observers) CPSC 329: Software Development Fall 2017 130 CPSC 329: Software Development Fall 2017 132
Model-View-Controller can utilize Strategy pattern with controllers to reuse same view with different models The ideas of MVC can be applied at different levels. organizing the whole program individual components e.g. JSlider, JList, JButton,... (technically Swing's version is a separable model architecture because view and controller are combined into one) CPSC 329: Software Development Fall 2017 133 CPSC 329: Software Development Fall 2017 135 fully generalized, every part view, controller, model is hidden behind an interface to decouple from specific concrete classes Views don't have to be user interfaces as such. e.g. how could MVC be used to allow one Game class to work with both standalone and network versions? CPSC 329: Software Development Fall 2017 134 CPSC 329: Software Development Fall 2017 136
Pattern: Visitor Intent. define a new operation without changing the classes of the elements on which it operates. a shopping cart containing a list of items would like to calculate postage costs for the contents of the cart calculate total order amount for the contents of the cart print the contents of the cart CPSC 329: Software Development Fall 2017 141 CPSC 329: Software Development Fall 2017 143 Pattern: Visitor Intent. define a new operation without changing the classes of the elements on which it operates Code structure. Visitor has a method for each type of element concrete visitors are the operations ObjectStructure is the collection of elements can define other visitors e.g. to print cart CPSC 329: Software Development Fall 2017 142 CPSC 329: Software Development Fall 2017 http://java.dzone.com/articles/design-patterns-visitor 144
Pattern: Visitor Notes. variation is in the action being done, not the traversal itself requires that things being visited be designed for the pattern, but additional changes for new visitors is not required new types of things being visited requires changes to the Visitor interface (or use of reflection) Visitor pattern is best when new actions are more likely than new things define CD, DVD, etc similarly CPSC 329: Software Development Fall 2017 145 CPSC 329: Software Development Fall 2017 147 s compilers processing of abstract syntax tree during code generation HTML/XML document formatting double dispatch - because overloaded methods are resolved at compile time visitor.visit(item) wouldn't compile CPSC 329: Software Development Fall 2017 146 CPSC 329: Software Development Fall 2017 148