Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 4 Adriana Ispas
Today s Exercise Session Assignment III Walkthrough the master solution (your solutions) Questions Assignment IV Calculator & Streams Pattern of the Day The Observer Pattern Quizzes 2
Assignment III Walkthrough the master solution (your solutions) Questions 3
Assignment IV Calculator Component & Streams 4
The Observer Pattern Behavioral pattern Thus concerned with communication between objects. Defines the way a number of classes can be notified of a change Define a one-to-many dependency between objects. A Subject may have any number of dependent Observers When the Subject object changes state, all its Observers are notified and updated automatically Also known as Each Observer queries the Subject to synchronize its state with the Subject's state. Dependents, Publish-Subscribe. 5
The Observer Pattern - Structure 6
The Observer Pattern - Collaborations 7
The Observer Pattern - Example Display data in more than one form at the same time and have all of the displays reflect any changes in that data. 8
The Observer Pattern - Example Separate the object containing the data from the objects displaying the data and make display objects observe changes in that data. Refer to the data as the Subject and each of the displays as Observers. Each of the observers registers its interest in the data by calling a public method in the Subject. 9
The Observer Pattern - Example 10
Watching Colors Change 11
Watching Colors Change Subject 12
Watching Colors Change Observers 13
The Observer Pattern JDK Implementation java.util.observable java.util.observable#notifyobservers() java.util.observable#notifyobservers(java.lang.object) java.util.observer java.util.observer#update(java.util.observable, java.lang.object) 14
The Observer Pattern Consequences 1 Observers promote abstract coupling to Subjects. A subject doesn t know the details of its observers. Potential disadvantage of successive or repeated updates to the Observers. If the cost of these updates is high, it may be necessary to introduce some sort of change management, so that the Observers are not notified too soon or too frequently. 15
The Observer Pattern Consequences 2 Need to decide which object initiates the notification of the change to the other observers. Subject notifies all the observers when one client changes the data. Client is not responsible for remembering to initiate the notification. But this can result in a number of small successive updates being triggered. Clients tell the Subject when to notify the other clients. But the clients are left with the responsibility of telling the Subject when to send the notifications. If one client forgets the program simply won t work properly. 16
The Observer Pattern Consequences 3 May specify different kinds of notifications by defining a number of update methods for the Observers to receive depending on the type or scope of change. In some cases, the clients will thus be able to ignore some of these notifications. 17
The Observer Pattern Consequences 4 Obtaining additional information about the change. Push model Subject sends detailed information about the change to the Observers (as argument to update). A superset of information including everything any Observer might need is transferred. Pull model Subject sends nothing but the most minimal notification, and Observers ask for details explicitly (via getstate()). Potential problems in asynchronous / concurrent environments if Subject's state changes again between the original announcement and the time the Observer issues its query. 18
Quiz 1:What is Printed? 19
Quiz 1:What is Printed? Output: false Name class does not override hashcode, thus it inherits the implementation from Object. Hash set chooses the hash bucket based on the hash value of the instance, as computed by the hashcode method. Second instance is distinct from the first, thus likely to have a different hash value. If the two hash values map to different buckets, the contains method will return false. Fix Override hashcode whenever you override equals. Ex: 37 * first.hashcode() + last.hashcode(); 20
Quiz 2:What Does the Program Print? The Basenji is a breed of small, curly-tailed dogs of African origin that do not bark. 21
Quiz 2:What Does the Program Print? Output: woof woof There is no dynamic dispatch on static methods. But bark is a static method. When a program calls a static method, the method to be invoked is selected at compile time, based on the compiletime type of the qualifier (the name we give to the part of the method invocation expression to the left of the dot). In this case, the qualifiers of the two method invocations are the variables woofer and nipper of type Dog. The invoked method is Dog.bark (it doesn't matter that the runtime type of nipper is Basenji; only its compiletime). Never qualify a static method invocation with an expression. 22
Quiz 3: Null and Void 23
Quiz 3: Null and Void Output: Hello world! Null.greet is a static method. Bad idea to use an expression as the qualifier in a static method invocation. The run-time type of the object referenced by the expression's value plays no role in determining which method gets invoked, nor does the identity of the object. In this case, there is no object, but that makes no difference. A qualifying expression for a static method invocation is evaluated, but its value is ignored. There is no requirement that the value be non-null. 24
Questions? 25
Exercise Session 5 Pattern of the Day The Singleton Pattern Quizzes Assignment IV Correction Assignment V Hand-out 26