Docsity

Event-Driven Programming and Model-View-Controller Pattern in Java, Study notes of Computer Science

Partial preview of the text

Download Event-Driven Programming and Model-View-Controller Pattern in Java and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

GUI II

Department of Computer ScienceUniversity of Maryland, College Park

Overview

Event-driven programming Model-View-Controller (MVC) Pattern GUI elements Java GUI classes JTables

Graphical User Interface (GUI) Goal Present information to users clearly & concisely Make interface easy to use for users Make software easy to implement / maintain forprogrammers Design issues Ease of use Ease of understanding Ability to convey information Maintainability Efficiency

Event-driven Programming Normal (control flow-based) programming Approach Start at main() Continue until end of program or exit() Event-driven programming Unable to predict time & occurrence of event Approach Start with main() Build GUI Await events (& perform associated computation)

Event-driven Programming in Java Example listeners & actions causing event^ ActionEvent

โ‡’โ‡’โ‡’โ‡’^ clicking button in GUI CaretEvent

โ‡’โ‡’โ‡’โ‡’^ selecting portion of text in GUI FocusEvent

โ‡’โ‡’โ‡’โ‡’^ component gains / loses focus KeyEvent

โ‡’โ‡’โ‡’โ‡’^

pressing key ItemEvent

โ‡’โ‡’โ‡’โ‡’^ selecting item from pull-down menu MouseEvent

โ‡’โ‡’โ‡’โ‡’^ dragging mouse over widget TextEvent

โ‡’โ‡’โ‡’โ‡’^ changing text within a field WindowEvent

โ‡’โ‡’โ‡’โ‡’^ closing a window

Model-View-Controller (MVC) Pattern^ Developed at Xerox PARC in 1978^ Separates GUI into 3 components^ Model

โ‡’โ‡’โ‡’โ‡’^ application data View^

โ‡’โ‡’โ‡’โ‡’^ visual interface Controller

โ‡’โ‡’โ‡’โ‡’^ user interaction Model

View Controller

MVC Pattern โ€“ Advantages Separates data from its appearance More robust Easier to maintain Provides control over interface Easy to support multiple displays for samedata^ Model^ Model

GUI GUI GUI

MVC Pattern โ€“ Model

Contains application & its data Provide methods to access & update data Interface defines allowed interactions Fixed interface enable both model & GUIs to beeasily pulled out and replaced Examples^ Text documents^ Spreadsheets^ Web browser^ Video games

MVC Pattern โ€“ Controller

Users interact with the controller Interprets mouse movement, keystrokes, etc. Communicates those activities to the model Interaction with model indirectly causes view(s)to update

The Model View Controller Song Lyrics and music by James Dempsey. Model View, Model View, Model View ControllerMVC's the paradigm for factoring your code, into functional segmentsso your brain does not explode.To achieve reusability you gotta keep those boundaries clean, Modelon the one side, View on the other, the Controller's in between.Model View - It's got three layers like Oreos do.Model View creamy ControllerModel objects represent your applications raison dโ€™ere. Customclasses that contain data logic and et cetra.You create custom classes in your app's problem domain, then youcan choose to reuse them with all the views, but the model objectsstay the same

Principles of GUI Design

Model is separate^ Never mix model code with GUI code^ View should represent model as it really is^ Not some remembered status In GUIโ€™s, user code for view and controller tendto mingle^ Especially in small programs^ Lot of the view is in system code

Do you have a good model? Could you reuse the model if you wanted toport the application to: a command-line textual interface an interface for the blind an iPod a web application, run on the web server, accessedvia a web browser