CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)

Slides:



Advertisements
Similar presentations
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Advertisements

The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
Written by: Dr. JJ Shepherd
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Stacks.
Design Patterns 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Problem Of The Day  Decipher the following phrase: STANDS 0 _
CS 537 Group 3 Report: Activity Diagrams and Observer Design Pattern Anna Deghdzunyan Xuan Yang Keenan Knaur John Hurley.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
CSC 213 – Large Scale Programming. Why Do We Test?
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
CSC 313 – Advanced Programming Topics. Design Pattern Intent  Each design pattern is a tool  Like all tools, have reason for being.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Problem of the Day  Why are manhole covers round?
1 Web Based Programming Section 8 James King 12 August 2003.
CSC 313 – Advanced Programming Topics. Open-Closed Principle Classes should be open for extension, but closed to modification  So, what does this mean?
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
CSC 313 – Advanced Programming Topics. Decorator Pattern Intent.
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
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.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Observer Pattern.
Written by: Dr. JJ Shepherd
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
21-Jun-16 Swing Basics and Model-View- Controller.
Behavioral Patterns Algorithms and the assignment of responsibilities among objects Describe not just patterns of objects or classes but also the patterns.
Observer Pattern Context:
Observer Design Pattern
Design Patterns Model – View – Controller
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
null, true, and false are also reserved.
Introduction to Behavioral Patterns (1)
Inner Classes 29-Nov-18.
CSE 331 Software Design & Implementation
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Advanced ProgramMING Practices
Inner Classes 1-May-19.
CSE 331 Software Design & Implementation
Software Design Lecture : 40.
Presentation transcript:

CSC 313 – Advanced Programming Topics

Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s) occurs  Can create many, many possible actions to occur  Update which actions used throughout execution  Can also use to break mutual dependency  UML class diagram cycles split and managed  Dirty & ugly hack that also is incredibly useful

Registering an Observer  Subject Observer  Subject adds & removes Observers as needed  Adding is simple  Events also simple, call Observers’ update method  Removing not as simple, but still not very hard  Requires some means of holding Observers  Array  ArrayList or LinkedList  HashMap

Registering an Observer

Getting the Word Out Observer  Calling Observers’ update methods is simple Observer  But for call Observer needs associated information  Not always clear what is best way to share this data  Must understand strengths & weaknesses of each  Push model is easiest of two approaches  ObserverSubject  Observers get data directly from Subject  Method’s parameters have values to be used  Another approach using pattern’s pull model  Subject Observer  Subject provides strategy to Observers via a param Observer  To get data, Observer uses strategies methods

Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener s  Tracking mouse in Swing using Observer Pattern  Much of the multi-threaded API uses this  Event-based code needs Observers to work  Not limited to Java; true for most OO languages

java.util.Observable  Among original classes included in Java  Works with java.util.Observer interface  ObservableObserver  Observable manages Observer s in code Observer Observer void addObserver(Observer o) void deleteObserver(Observer o) void deleteObservers() int countObservers()

Handling Updates  Observable  Observable approach supports push… void notifyObserver(Object arg) … & pull model available as well void notifyObserver() boolean hasChanged() void clearChanged() void setChanged() Observer ’s  Notifications sent to Observer ’s only method Observable void update(Observable o, Object arg)

Observable Using Observable Problem public class CSCClass { private Observable obs; // Lots of unimportant code here public void cancelClass() { obs.notifyObservers(“Go drinking!”); } }

Using Observable Problem public class CSCClass { private Observable obs; // Lots of unimportant code here public void cancelClass() { obs.notifyObservers(“Go drinking”); } }

Using Observable Problem public class CSCClass { private Observable obs; // Lots of unimportant code here public void cancelClass() { obs.notifyObservers(“Go drinking”); } }

Bad & Worse Design Decisions  No update was sent

Bad & Worse Design Decisions  No update was sent; sobriety ruled the day

Bad & Worse Design Decisions  No update was sent; sobriety ruled the day

Bad & Worse Design Decisions

Updated Code

Next Attempt public class CSCClass { private Observable obs; // Lots of unimportant code here public void cancelClass() { obs.setChanged(); obs.notifyObservers(“Go drinking”); } }

Next Attempt

setChanged has protected access

Awful Design

setChanged & clearChanged protected access java.util package is sealed; cannot write class for it Can only be called & used from within subclass Observable Must write subclass of Observable to use it As this is Java, that class cannot extend others Observer Updates received in method of Observer : void update(Observable o, Object arg) This really means that can only push 1 object Kills point of push model since cannot push much

Awful Design setChanged & clearChanged protected access java.util package is sealed; cannot write class for it Can only be called & used from within subclass Observable Must write subclass of Observable to use it As this is Java, that class cannot extend others Observer Updates received in method of Observer : void update(Observable o, Object arg) This really means that can only push 1 object Kills point of push model since cannot push much Useless

Final Attempt public class CSCClass extends Observable { public void cancelClass() { setChanged(); notifyObservers(“Go drinking”); } } public class Student implements Observer { public void update(Observable o, Object arg) { String s = (String)arg; if (!s.equals(“Get here NOW!!!”)) // Go drinking else // Go to class } }

Final Attempt Observable public class CSCClass extends Observable { public void cancelClass() { setChanged(); notifyObservers(“Go drinking”); } } Observer public class Student implements Observer { public void update(Observable o, Object arg) { String s = (String)arg; if (!s.equals(“Get here NOW!!!”)) // Go drinking else // Go to class with a drink } }

Final Attempt

Observable Pros & Cons  Cons:  Pros:

Observable Pros & Cons  Cons: Observable  Need a subclass of Observable to use  Must call setChange before notifyObservers Observer  Can push only 1 object to Observer s  Pros: Observer  Already has code to add & remove Observer s  Easy to rewrite and improve upon this design

For Next Lecture  Two (short) readings available on web  Will start looking into how code is optimized  How does Java run & make programs faster?  What can we do to make our code faster?  Using final can be huge. Why would it matter?  Lab #2 due at week’s end & still on Angel  Make sure that you also check the grading rubric  Use Assignment Submitter when turning it in