Software design - Lecture 40. The main topics covered in this chapter include: observer design pattern or publish and subscribe; motivation for observer design pattern; one data multiple representations; newspaper subscription example; case for observer design pattern;...
Trang 1Lecture : 40
Trang 2OR
Publish and Subscribe
Trang 3Pattern
When we partition a system into a collection of cooperation classes, it is desired that consistent state between participating objects is to be maintained
This should be not achieved via tight coupling as against our basis design principle because for obvious reason this will reduce reusability
Trang 4One Data multiple Representations
Trang 5Bar Graph and Pie Chart don’t know about each other so that any one can be reused independent of each other, but the interesting thing is that it seems that they know each other. How???
When the data in the spreadsheet is changed it is reflected in pie chart and bar graph also immediately
•
Trang 6This behavior implies that they are dependent on data of the spreadsheet and when ever there is a change in spreadsheet pie chart and bar graph is notified to update the change.
There seems to be no reason to believe that the number
of objects representing the data is to be limited may be i
e may be line graph is to be used in future to represent data
Trang 8 The set of dependent objects are referred to as
Observers ie Graphs in our example
The object on which Observer dependent is
referred to as the subject. ie Spreadsheet in our
example
Trang 9Newspaper Subscription Example
Trang 10Observer pattern suggests a publishersubscriber model
leading to a clear boundary between the set of Observer objects and the Subject object
A typical observer is an object with interest or dependency in the state of the subject
Trang 12In other words, the scenario contains a onetomany relationship between a subject and the set of its observers
Each of the observer objects has to register itself with the subject to get notified when there is a change in the subject’s state
Trang 14“This pattern defines a onetomany relationship between objects so that when there is change in the state of the one object it should be notified and automatically updated
to all of it’s dependent”
Trang 15In the push model — The subject should send the state information that the observers may be interested in.
Trang 16Class Diagram
Trang 18Sequence Diagram
Trang 20 Support for event broadcasting
Minimal coupling between the Subject and the Observer . Reuse obervers without using subject and vice verca
Trang 21 Liabilities:
Possible cascading of notifications
Observers are not necessarily aware of each other and must be careful about triggering updates
Trang 22 Java provides the Observable/Observer classes as builtin support for the Observer pattern.
Trang 23 The java.util.Observer interface is the Observer
interface.
It must be implemented by any observer class.
Trang 24Sample Code
Trang 28public PriceObserver() {
price = 0;
System.out.println("PriceObserver created: Price is " + price);}
public void update(Observable obj, Object arg) {
if (arg instanceof Float) {
price = ((Float)arg).floatValue();
System.out.println("PriceObserver: Price changed to " + price);} else {
System.out.println(”PriceObserver: Some other change to
subject!"); }
}