Unformatted text preview:

Observer Observable COMP 401 Spring 2013 Lecture 19 3 26 2013 Observer Observable O cial Gang of Four descripDon De ne a one to many dependency between objects so that when one object changes state all its dependents are noD ed and updated automaDcally Observable The object of interest Represents data or state that may change in the future Observer The interested objects Represents parts of system that need to be updated or may need to take acDon if when observable object changes COMP 401 Spring 2012 2 Use Cases User Interfaces User interface elements like buWons scrollbars etc are observable State changes correspond to clicks drags etc ApplicaDon objects that must respond to user s interacDons with the UI are observers Asynchronous Programming Also known as event based programming May have well de ned acDons corresponding to events that may occur but can t know in advance which event will occur or when it will occur COMP 401 Spring 2012 3 Basic Observer Observable class Observable List Observer observers void addObserver Observer o Adds o to list of observers observers add o void deleteObserver Observer o Takes o o list of observers observers remove o void noDfyObservers Trigger update method on all observers for Observer o observers o update interface Observer void update De ning Observer as an interface allows any class to act as an observer for the Observable class NoDce that Observable can have more than one observer And that they don t know about each other or the order of update COMP 401 Spring 2012 4 lec19 v1 Game acts as Observable class GameObserver acts as Observer interface Discussion How many games can one fan watch What if we wanted to have fan objects that could watch more than one game simultaneously Observer Observable Re ned class Observable List Observer observers void register Observer o Adds o to list of observers observers add o void unregister Observer o Takes o o list of observers observers remove o void noDfyObservers Trigger update method on all observers for Observer o observers o update this interface Observer void update Observable o Passing reference to observable as a parameter to update method allows Observer to register with more than one Observable and then detect which one changed COMP 401 Spring 2012 6 lec19 v2 Same observers watching mulDple games Game object passed to fan as parameter to update method So far fans are reacDng to current state of game But in real life what do we react to COMP 401 Spring 2012 7 Observer Observable Re ned More class Observable List Observer observers void register Observer o Adds o to list of observers observers add o void unregister Observer o Takes o o list of observers observers remove o void noDfyObservers Info i Trigger update method on all observers for Observer o observers o update this i interface Observer void update Observable o Info i In addiDon to passing reference to Observable that changed we can encapsulate what changed as addiDonal informaDon for the update method of the Observer COMP 401 Spring 2012 8 lec19 v3 GameObserver update method now takes second argument String who scored This is the info about the change in the game that is being passed to the fans Observer Observable in java uDl Java provides skeleton Observer Observable that you can extend in java uDl Don t have to use them Our examples so far haven t The paWern is de ned by relaDonship between objects and their interacDon Not the speci c method names and or implementaDon Should be able to recognize Observer Observable by these characterisDcs Observer object some how registers with observable Observable invokes a method on observers in order to signal state changes COMP 401 Spring 2012 10 lec19 v4 Game extends java uDl Observable No longer have to manage our own list since we inherit that from Observable UNCFan and DukeFan implement java uDl Observer Parameters to update are no longer context speci c update Observable o Object arg Need contravariant cast to make them useful Observable parent class requires state change to be signaled by call to setChanged before calling noDfyObservers Otherwise won t do anything DelegaDng java uDl Observable Using Observable is convenient Inherits basic Observable funcDonality without having to worry about doing it yourself Must subclass observable Suppose your class already has a parent due to design or circumstance Don t have development control over class hierarchy Parent class is part of third party library SoluDon lies with delegaDon COMP 401 Spring 2012 12 DelegaDng Observable Outline of soluDon Create a helper class that does subclass Observable Implements same interface as original class via delegaDon Override noDfyObservers to call setChanged rst Create an instance of this helper class as part of your object Provide methods from Observable via delegaDon to this instance DelegaDon is being used both ways here Allows Observer to use hidden observable instance as if it were the original object Allows outside code to interact with original object as if it were Observable lec19 v5 COMP 401 Spring 2012 13 MulDple Observer Lists Same delegaDon trick can be used to support mulDple lists of observers Useful if you want to support more than one type of observable changes events Requires providing a way to specify what is being observed One approach separate registraDon methods for each observable change event Another approach addiDonal argument to registraDon method May need to provide way of disDnguishing between events as part of update info if you want the same observer to be able to observe more than one observable event lec19 v6 COMP 401 Spring 2012 14


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lecture19ObserverObservable

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lecture19ObserverObservable and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lecture19ObserverObservable and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?