Unit 4 Flashcards
(18 cards)
What is imperative programming?
Program flow is specified by the programmer
Why might imperative programming be the best solution to all problems?
Inflexible sequence: cannot act “out of order”, user must respond in the right way
No concurrency: can’t act “out of turn”, must wait for user to input/respond
Users with multiple possible inputs (mouse, keystrokes, button presses, etc.)
Multiple users
Things happening without user input: file downloads complete, time elapses
What is event driven programming?
Program flow/execution is driven by events
Program executes by detecting and identifying an event, then running a callback function which determines what to do when the event occurs
Why might event-driven programming not be ideal in all cases?
The code executes indirectly
- The chain of cause/effect can grow deep
- Code does not execute sequentially
- Requires a strong mental model and naming scheme
Increased overhead for execution
- Events are messages which must be created and passed
- Flexibility in exchange for speed of execution
What is the “observer” pattern?
A one-to-many event driven model
- One subject (event generator)
- One or more observers (handlers)
Observers subscribe to a subject by registering
Subject notifies all subscribed observers
What is MVC?
Model-View-Controller
Model: store the state of your program (data)
View: display the state of your program to the user (output)
Controller: change the state of your program (input)
Describe the Model component in MVC
- Model is the state of the world
– Data describing the world of your program - Specific to the situation or entities you are modelling
– Data describing the state of your program - Is the user authorized? Have they asked to quit?
- Model is built out of
– Data structures (COSC 2010)
– Program execution state
Describe the View component in MVC
- View is what the user sees
– Represents the current state of the model (or part of it)
– Communicates program and data tu user
– Could be CLI, GUI, hardware, etc. - If the model changes, the view becomes out-of-date (stale)
– A push approach - Model change event -> view update (to capture new model)
– A pull approach - View refresh event -> view update to capture new model
Describe the Controller component in MVC
- Controller is what changes the model
– Controller code adds/edits/removes data from the model
– Alters or updates program state - Typically part of an event callback
– User input event -> callback that modifies the model
– External signal event -> modify the model - Image download complete -> replace “placeholder” image with real one
What are the pros and cons of Swing?
Pros: appearance can be customized and/or standardized across OS
Cons: can be slower (if we’re careless), “near-match” native appearance
True or False: Swing extends AWT?
True
AWT handles OS interactions
Swing provides customization
Describe Swing containers
Containers hold/organize components (including other containers)
Top level containers can “stand alone”
- JFrame: window with title border
- JWindow: window without title or border
- JDialog: popup, usually to get input
- JPanel: simplest container, cannot be top-level container
What is a content pane?
Swing containers all have a content pane
It holds the components within the pane
Manages how they are arranged and drawn
What is a layout manager?
It arranges components within a container (content pane)
Layout manager tracks what’s been added, what takes up space, what is visible
What are the five “regions” in a container?
North
South
East
West
Center
Note: regions are greedy; if not occupied, other regions grow to fill
What does “enabling” a JButton mean?
Instantiate a JButton (add to layout)
Instantiate an ActionListener
JButton.addActionListener(ActionListener)
True or False: paint() is invoked automatically whenever the component needs to be redrawn
True
What three steps does Swing break the paint process into?
paintComponent : paint yourself
paintBorder : paint your edges
paintChildren : paint your contained components