Download presentation
Presentation is loading. Please wait.
Published byEgbert Dorsey Modified over 9 years ago
1
Week 5, Day 3: Observer Today Reducing coupling with the Observer The Observer pattern in Java APIs Posting events to a UI worker thread SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1
2
Reducing Coupling with the Observer Consider this application: Wrapper class access a weather page and provides: Current weather 14-day forecast 60-day weather history Discuss: How can you minimize the coupling between a subject and an observer in this class? SE-2811 Dr. Mark L. Hornick 2
3
Java’s Observer Patterns Java has multiple implementations of the Observable/Observer pattern java.util: Observable Observer javax.swing javax.swing.AbstractButton java.awt.EventListener SE-2811 Dr. Mark L. Hornick 3
4
java.util.Observable This “Subject” is a class Advantages: Generic implementation (can’t implement in interface) Generic methods available Disadvantages: If you want to inherit from a different class… you can’t SE-2811 Dr. Mark L. Hornick 4
5
Connecting Worker Threads w. the Event Dispatch Thread Sometimes we have a lot of work to do “behind the scenes” Once the work is done, we want to notify the event dispatch thread. Solution:SwingUtilities.invokeLater() executes a runnable at the end of the current dispatch list Pick one: Runnable should be Brief, like a button-press handler Long, like a full thread 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.