MoreDesignPatterns

MoreDesignPatterns - CSE 219 Patterns in Programming More...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 219 Patterns in Programming More Design Patterns
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reading Assignment Why Software Fails from IEEE Spectrum: http://www.spectrum.ieee.org/sep05/1685 Software Hall of Shame http://www.spectrum.ieee.org/sep05/1685/failt1
Background image of page 2
GUIs & Patterns Patterns are commonly used throughout GUI packages wide use of interfaces Command Pattern - Event Programming for simple GUI controls Observer Pattern - Event Handling for complex GUI controls (MVC) Strategy Pattern for Layout Management
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Command Abstraction For many GUIs, a single function may be triggered by many means (e.g., keystroke, menu, button, etc…) Try to link all similar events to the same listener The information concerning the command can be abstracted to a separate command object Two Common Java Approaches: 1. Specify a String for each command have listener respond to each command differently 2. Make an Action object for each command make each command a listener for the events that trigger it Both techniques: ensure commands are handled in a uniform way require access to the necessary GUI controls to effect change
Background image of page 4
Example Suppose I wanted to create a simple GUI: 1 colored panel 2 buttons, yellow & red 2 menu items, yellow & red clicking on the buttons or menu items changes the color of the panel Since the buttons & the menu items both perform the same function, they should be tied to the same commands I could even add popup menu items
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
First Approach – Command Strings public class ColorCommandFrame1 extends JFrame implements ActionListener { private Toolkit tk = Toolkit.getDefaultToolkit(); private ImageIcon yellowIcon = new ImageIcon(tk.getImage("yellow_bullet.bmp")); private ImageIcon redIcon = new ImageIcon(tk.getImage("red_bullet.bmp")); private JPanel coloredPanel = new JPanel(); private JButton yellowButton = new JButton(yellowIcon); private JButton redButton = new JButton(redIcon); private JMenuBar menuBar = new JMenuBar(); private JMenu colorMenu = new JMenu("Color"); private JMenuItem yellowMenuItem = new JMenuItem(yellowIcon); private JMenuItem redMenuItem = new JMenuItem(redIcon); private JPopupMenu popupMenu = new JPopupMenu(); private JMenuItem yellowPopupItem = new JMenuItem(yellowIcon); private JMenuItem redPopupItem = new JMenuItem(redIcon); private static final String YELLOW_COMMAND = "YELLOW_COMMAND"; private static final String RED_COMMAND = "RED_COMMAND";
Background image of page 6
public ColorCommandFrame1() {
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2009 for the course CSE 219 taught by Professor Mckenna during the Spring '08 term at SUNY Stony Brook.

Page1 / 24

MoreDesignPatterns - CSE 219 Patterns in Programming More...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online