Exercise Session 4 Adriana Ispas Todays Exercise Session - - PowerPoint PPT Presentation

exercise session 4
SMART_READER_LITE
LIVE PREVIEW

Exercise Session 4 Adriana Ispas Todays Exercise Session - - PowerPoint PPT Presentation

Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 4 Adriana Ispas Todays Exercise Session Assignment III Walkthrough the master solution (your solutions) Questions


slide-1
SLIDE 1

Languages in Depth Series: Java Programming

  • Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Exercise Session 4

Adriana Ispas

slide-2
SLIDE 2

2

Languages in Depth series: Java Programming

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

slide-3
SLIDE 3

3

Languages in Depth series: Java Programming

Assignment III

Walkthrough the master solution (your solutions) Questions

slide-4
SLIDE 4

4

Languages in Depth series: Java Programming

Assignment IV

Calculator Component & Streams

slide-5
SLIDE 5

5

Languages in Depth series: Java Programming

The Observer Pattern

Behavioral pattern

  • Thus concerned with communication between objects.

Defines the way a number of classes can be notified

  • f 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

  • Each Observer queries the Subject to synchronize its

state with the Subject's state.

Also known as

  • Dependents, Publish-Subscribe.
slide-6
SLIDE 6

6

Languages in Depth series: Java Programming

The Observer Pattern - Structure

slide-7
SLIDE 7

7

Languages in Depth series: Java Programming

The Observer Pattern - Collaborations

slide-8
SLIDE 8

8

Languages in Depth series: Java Programming

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.

slide-9
SLIDE 9

9

Languages in Depth series: Java Programming

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.

slide-10
SLIDE 10

10

Languages in Depth series: Java Programming

The Observer Pattern - Example ……

slide-11
SLIDE 11

11

Languages in Depth series: Java Programming

… Watching Colors Change

slide-12
SLIDE 12

12

Languages in Depth series: Java Programming

Watching Colors Change – Subject

slide-13
SLIDE 13

13

Languages in Depth series: Java Programming

Watching Colors Change – Observers

slide-14
SLIDE 14

14

Languages in Depth series: Java Programming

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)


slide-15
SLIDE 15

15

Languages in Depth series: Java Programming

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.

slide-16
SLIDE 16

16

Languages in Depth series: Java Programming

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.

slide-17
SLIDE 17

17

Languages in Depth series: Java Programming

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.

slide-18
SLIDE 18

18

Languages in Depth series: Java Programming

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.

slide-19
SLIDE 19

19

Languages in Depth series: Java Programming

Quiz 1:What is Printed?

slide-20
SLIDE 20

20

Languages in Depth series: Java Programming

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();

slide-21
SLIDE 21

21

Languages in Depth series: Java Programming

Quiz 2:What Does the Program Print?

The Basenji is a breed of small, curly-tailed dogs of African origin that do not bark.

slide-22
SLIDE 22

22

Languages in Depth series: Java Programming

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.

slide-23
SLIDE 23

23

Languages in Depth series: Java Programming

Quiz 3: Null and Void

slide-24
SLIDE 24

24

Languages in Depth series: Java Programming

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.
slide-25
SLIDE 25

25

Languages in Depth series: Java Programming

Questions?

slide-26
SLIDE 26

26

Languages in Depth series: Java Programming

Exercise Session 5

Pattern of the Day

  • The Singleton Pattern

Quizzes Assignment IV

  • Correction

Assignment V

  • Hand-out