This preview shows page 1. Sign up to view the full content.
Unformatted text preview: nteraction diagram of Observer return subjectState; observerState = subject->getState(); Update() GetState() forall o in list o->update(); 105 106 Observer pattern (cont
Consequences Pattern d) Pattern Observer pattern (cont
Implementation issues 1. Storing list of observers Typically, in subject d) 1. Low coupling between subject and observers Subject unaware of number, location, and types of dependents Subject and observers can belong to different system components Important for Model-View-Controller (MVC) paradigm 2. Support for broadcasting Dynamic addition and removal of observers 3. Unexpected run-time penalty No control by subject on observer computations 107 Expensive when many subjects have no observers Alternatively, use lookup table (a hash table?) 2. Observing multiple subjects Typically, add parameters to update() 3. Who triggers update? Subject: Possibly too many updates Observers: Error-prone if observer forgets to send notiGication message 108 Pattern Pattern Observer pattern (cont d) Observer pattern (cont d) Implementation issues (cont d) 4. Possibility of dangling references when subject is deleted (e.g., because observer must keep reference to subject) Go4: Easier in garbage-collected languages (e.g., Smalltalk, Java, etc.) But is it really? Perhaps an application of weak references Weak reference: One that is not used to prevent an object from being garbage collected Otherwise, have subject notify observers before dying Implementation issues (cont d) 5. Possibility of premature notiGications Typically, method in Subject subclass calls inherited method which does notiGication Solve by using Template method pattern (method in abstract class calls deferred methods, which is deGined by concrete subclasses) 109 110 Pattern Pattern Observer pattern Implementation issues (cont d) 6. How much information should subject send with update() messages? Push model: Subject always sends all information that observers may ever need Disadvantage: May couple subject with observers (by forcing a given observer interface) Pull model: Subject sends no information Disadvantage: Can be inefGicient 111 Observer pattern Implementation issues (cont d) 7. Registering o...
View Full Document
This note was uploaded on 02/23/2012 for the course COMP 553 taught by Professor Ajay during the Spring '12 term at Ill. Chicago.
- Spring '12