Doug Jeffries CS490 Design Patterns May 1, 2003

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Object-Oriented Analysis and Design
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 TCSS 360, Spring 2005 Lecture Notes Design Patterns: Factory, Command.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Unit 22 Command Summary prepared by Kirk Scott 1.
Command. RHS – SOC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Lexi case study (Part 2) Presentation by Matt Deckard.
1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Unit 14 Command Summary prepared by Kirk Scott 1.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Frameworks CompSci 230 S Software Construction.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Computer Science 313 – Advanced Programming Topics.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS 415 N-Tier Application Development By Umair Ashraf June 25,2013 National University of Computer and Emerging Sciences Lecture # 4 Web Presentation Patterns.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
The Command Pattern SE-2811 Dr. Mark L. Hornick 1.
Command. RHS – SWC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Chapter 32 JavaBeans and Bean Events
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Summary prepared by Kirk Scott
CompSci 230 S Programming Techniques
Observer Pattern Context:
Chapter 36 JavaBeans and Bean Events
A First Look at GUI Applications
Advanced User Interfaces
Lecture 09 Applets.
GUI III IS
Behavioral Design Patterns
Review: Java GUI Programming
Singleton Pattern Command Pattern
Object-Oriented Programming
Programming Design Patterns
Graphical User Interfaces in Java Event-driven programming
1 Graphical User Interfaces
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Doug Jeffries CS490 Design Patterns May 1, 2003 Command Doug Jeffries CS490 Design Patterns May 1, 2003

Overview What is Command? UML for Command interface Examples Challenge problems

What is Command? GOF definition Object-oriented version of callbacks Encapsulate commands in objects so that you can control their selection, sequencing, queue them, undo them and otherwise manipulate them. Object-oriented version of callbacks Callbacks are functions registered to be called later

What is Command? Keeps the invoker of a function from needing a reference to the receiver which implements the action. Invoker often does not care Simply wants to issue abstract command Sometimes called Transaction or Action

UML

Example Restaurant Customer is the Invoker Waiter is the Command Taking the order is the execution of command Staff are the Receiver Cook prepares the ordered dish Janitor cleans floor after cook spills your food on the floor Waiter delivers it, and probably your bill Paying your bill is another command you will execute Cashier accepts payment Busboy clears the table when you finish Valet brings your car around

In the Wild Where have you seen this pattern?

In the Wild Waiter example javax.swing.Action JButton may be the Invoker no knowledge of Receiver Action is the Command provides actionPerformed() Action delegates to some Receiver

Vehicle Example

Metsker’s Challenge 24.1 The mechanics of Java menus make it easy to apply the COMMAND pattern, but they do not require that you organize your code as commands. In fact, it is common to develop an application in which a single object listens to all the events in a GUI. What pattern does that follow?

Metsker’s Solution 24.1 Java Swing applications commonly apply the MEDIATOR pattern, registering a single object to receive all GUI events. This object mediates the interaction of the components and translates user input into commands for business domain objects.

Metsker’s Challenge 24.2 Fill in the code for the anonymous subclasses of ActionListener, overriding actionPerformed(), noting that this method expects an ActionEvent argument.

Metsker’s Challenge 24.2 protected JMenu fileMenu() { if(fileMenu == null) { fileMenu = new JMenu("File"); Font f = SwingFacade.getStandardFont(); fileMenu.setFont(f); JMenuItem save = new JMenuItem("Save"); save.setFont(f); fileMenu.add(save); save.addActionListener(new ActionListener() { /* CHALLENGE! */ }); JMenuItem load = new JMenuItem("Load"); load.setFont(f); fileMenu.add(load); load.addActionListener(new ActionListener() } return fileMenu;

Metsker’s Solution 24.2 load.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) save(); // or load(); } });

Challenge 1 Compare Command to Strategy

Solution 1 Similarities Differences Both provide ways for actions to be performed abstractly, without having to care about the details of the implementer. Differences Strategy requires the invoker of the action to have a reference to the implementer

Challenge 2 How might Command make it easier to support an undo operation?

Solution 2 Your Command interface could be extended to have an unexecute() method, and state could be stored by the command object itself. Store previously executed commands in a history stack.

Challenge 3 Could logging functionality be added in a similar way as undo was in the previous challenge?

Solution 3 Yes. Simply add load() and save() to the Command interface. State can be restored by reloading the logged commands and executing them in the proper order.

Comments?