SYSC3110_Observer.ppt - SYSC-3110 Object-Oriented Programming and Design Laboratory The Observer Pattern a solution for decoupling(c Copyright Systems

SYSC3110_Observer.ppt - SYSC-3110 Object-Oriented...

This preview shows page 1 - 8 out of 21 pages.

(c) Copyright Systems and Computer Engineering, Carleton University 1 SYSC-3110 Object-Oriented Programming and Design Laboratory The Observer Pattern, a solution for decoupling
Image of page 1

Subscribe to view the full document.

2 A design problem Prof E. wants to notify the secretary and the students every time there is a new midterm. The secretary would then book the rooms and the students would (hopefully) prepare for the exam. 1st pass: the prof knows who is interested, and calls them directly: void setMidterm(…) { // let the secretary know mySecretary.bookRoom(…); // let the students know for (Student s : students ) {//students is some list s.study(…); } }
Image of page 2
3 A design problem problem: the Prof code is tightly coupled to the Student and Secretary classes what if Secretary or Student's public interfaces change? what if the prof also needs to notify a TeachingAssistant? Prof depends on the method to invoke on each interested party there must be a general design solution to this…
Image of page 3

Subscribe to view the full document.

4 Design Pattern Intent : how can I “define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically”? we want to notify objects: without having to know how many they are without having to know who they are basically we want to decouple the prof from the people who should be notified
Image of page 4
5 Observer Pattern Structure : a publish-subscribe mechanism: the subject a.k.a the observable maintains a list of observers the observers get added to that list by subscribing all observers implement a common interface, namely an update() method when a change occurs, the observable iterates through the list of observers and calls the update method on them -> defined in GoF
Image of page 5

Subscribe to view the full document.

6 Observer Pattern
Image of page 6
7 Observer Pattern
Image of page 7

Subscribe to view the full document.

Image of page 8

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes