Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nov 5, 200291.3913 Ron McFadyen1 Observer Pattern Objects that participate in the observer pattern must either be publishers (subject) or subscribers (observers)

Similar presentations


Presentation on theme: "Nov 5, 200291.3913 Ron McFadyen1 Observer Pattern Objects that participate in the observer pattern must either be publishers (subject) or subscribers (observers)"— Presentation transcript:

1 Nov 5, 200291.3913 Ron McFadyen1 Observer Pattern Objects that participate in the observer pattern must either be publishers (subject) or subscribers (observers) publishers publish events of interest to others notify objects, who previously subscribed, when events occur subscribers subscribe to information about these events these objects must be allowed to unsubscribe

2 Nov 5, 200291.3913 Ron McFadyen2 Observer Pattern Publishers must have Attach method (Larman text: addPropertyListener) Detach method Notify method (Larman text: publishPropertyEvent) Subscribers must have Update method (Larman text: onPropertyEvent)

3 Nov 5, 200291.3913 Ron McFadyen3 Observer Pattern Example: a special situation for courses is: when enrollment reaches the capacity and no more seats are available, the course is full. (for simplicity we ignore sections) Suppose when a course is full: The instructor is informed. The Registration Office is informed

4 Nov 5, 200291.3913 Ron McFadyen4 Observer Pattern Example: Suppose our class model is: DepartmentRegOffice Course * Faculty 0,1* 0..*

5 Nov 5, 200291.3913 Ron McFadyen5 Example: Filling out some methods Department RegOffice Course * 0..* update() Attach() Detach() Notify() checkFull() myObs 0,1 Instructor Faculty and RegOffice will be observers, and must implement the update method Course will be the subject, and will implement attach, detach, and notify methods. Course keeps track of its observers. 0..* Faculty update()

6 Nov 5, 200291.3913 Ron McFadyen6 Course > Observer * Recall from Larman that the subject is loosely couple to a set of observers; Course will have a set of references to observers. FacultyRegOffice Faculty and RegOffice must implement the Observer interface

7 Nov 5, 200291.3913 Ron McFadyen7 Example: interactions i:Facultyc:Course When the Instructor is assigned to a course, the Instructor must register his/her interest with the course attach(i) assign(c) myObs:Object The course adds the Instructor to its list add(i)

8 Nov 5, 200291.3913 Ron McFadyen8 Example: interactions c:CoursemyObs:Object The Registration Office registers its interest in the course add(r) r:RegOffice attach(r) newCourse(c) The course adds the Registration Office to its list

9 Nov 5, 200291.3913 Ron McFadyen9 Example: interactions – updates sent to two observers i:Facultyc:Courser:RegOffice update() [full] notify() checkFull() The course realizes it is full, and notifies its two observers The two objects will do whatever is appropriate for them when they are notified via the update message

10 Nov 5, 200291.3913 Ron McFadyen10 Example: interactions – similar to previous slide, but in more general terms c:CoursemyObs:Object * : update() [full] notify() checkFull() The course realizes it is full, and notifies its observers In general, the course sends the update message to each object that is registered for the event

11 Nov 5, 200291.3913 Ron McFadyen11 Observer Pattern Summary Responsibility for monitoring an event is with the object that creates the event (the Subject) Subject knows its observers (but not the class they belong to) Don’t need to modify the Subject if the observers change More detail needed if the subscribers need to get specific information after being notified


Download ppt "Nov 5, 200291.3913 Ron McFadyen1 Observer Pattern Objects that participate in the observer pattern must either be publishers (subject) or subscribers (observers)"

Similar presentations


Ads by Google