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 Languages in Depth series: Java Programming 2
Assignment III Walkthrough the master solution (your solutions) Questions Languages in Depth series: Java Programming 3
Assignment IV Calculator Component & Streams Languages in Depth series: Java Programming 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 Observer s When the Subject object changes state, all its Observers are notified and updated automatically Each Observer queries the Subject to synchronize its state with the Subject's state. Also known as Dependents, Publish-Subscribe. Languages in Depth series: Java Programming 5
The Observer Pattern - Structure Languages in Depth series: Java Programming 6
The Observer Pattern - Collaborations Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 9
The Observer Pattern - Example …… Languages in Depth series: Java Programming 10
… Watching Colors Change Languages in Depth series: Java Programming 11
Watching Colors Change – Subject Languages in Depth series: Java Programming 12
Watching Colors Change – Observers … Languages in Depth series: Java Programming 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) Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 18
Quiz 1:What is Printed? Languages in Depth series: Java Programming 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(); Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 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 compile- time 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 compile- time). Never qualify a static method invocation with an expression. Languages in Depth series: Java Programming 22
Quiz 3: Null and Void Languages in Depth series: Java Programming 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. Languages in Depth series: Java Programming 24
Questions? Languages in Depth series: Java Programming 25
Exercise Session 5 Pattern of the Day The Singleton Pattern Quizzes Assignment IV Correction Assignment V Hand-out Languages in Depth series: Java Programming 26
Recommend
More recommend