lecture 8: java’s observable & observer

27
LECTURE 8: JAVA’S OBSERVABLE & OBSERVER CSC 313 – Advanced Programming Topics

Upload: yetty

Post on 22-Feb-2016

69 views

Category:

Documents


0 download

DESCRIPTION

CSC 313 – Advanced Programming Topics. Lecture 8: Java’s Observable & Observer. Observer Pattern in Java. Java ♥ Observer Pattern & uses everywhere Find pattern in JButton & ActionListener s Tracking mouse in Swing using Observer Pattern Much of the multi-threaded API uses this - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 8: Java’s Observable & Observer

LECTURE 8:JAVA’S OBSERVABLE & OBSERVER

CSC 313 – Advanced Programming Topics

Page 2: Lecture 8: Java’s Observable & Observer

Observer Pattern in Java

Java ♥ Observer Pattern & uses everywhere Find pattern in JButton & ActionListeners

Tracking mouse in Swing using Observer Pattern

Much of the multi-threaded API uses this Event-based code needs Observers

to work Not limited to Java; true for most OO

languages

Page 3: Lecture 8: Java’s Observable & Observer

java.util.Observable

Among original classes included in Java Works with java.util.Observer interface

Observable manages Observers in codevoid addObserver(Observer o)void deleteObserver(Observer o)void deleteObservers() int countObservers()

Page 4: Lecture 8: Java’s Observable & Observer

Handling Updates

Observable will support push model…void notifyObserver(Object arg)

… and pull model can also be usedvoid notifyObserver()boolean hasChanged()void clearChanged()void setChanged()

Notifications sent to Observer’s only method

void update(Observable o, Object arg)

Page 5: Lecture 8: Java’s Observable & Observer

Using Observable Problem

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.notifyObservers(“Go ski”);}

}

Page 6: Lecture 8: Java’s Observable & Observer

Using Observable Problem

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.notifyObservers(“Go ski”);}

}

Page 7: Lecture 8: Java’s Observable & Observer

Using Observable Problem

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.notifyObservers(“Go ski”);}

}

Page 8: Lecture 8: Java’s Observable & Observer

Bad & Worse Design Decisions No update was sent

Page 9: Lecture 8: Java’s Observable & Observer

Bad & Worse Design Decisions No update was sent

Page 10: Lecture 8: Java’s Observable & Observer

Bad & Worse Design Decisions No update was sent; everyone went

to class

Page 11: Lecture 8: Java’s Observable & Observer

Bad & Worse Design Decisions No update was sent; everyone went to

class

notifyObservers misnamed horribly Should be notifyObserversIfChanged Must call setChanged before notifying

Use of notifyObservers inconsistent, too Calls clearChanged automatically once

updates sent

Page 12: Lecture 8: Java’s Observable & Observer

Updated Code

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.setChanged(); obs.notifyObservers(“Go ski”);}

}

Page 13: Lecture 8: Java’s Observable & Observer

Next Attempt

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.setChanged(); obs.notifyObservers(“Go ski”);}

}

Page 14: Lecture 8: Java’s Observable & Observer

Next Attempt

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.setChanged(); obs.notifyObservers(“Go ski”);}

}

Page 15: Lecture 8: Java’s Observable & Observer

Next Attempt

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.setChanged(); obs.notifyObservers(“Go ski”);}

}

Page 16: Lecture 8: Java’s Observable & Observer

Next Attempt

public class CSCClass {private Observable obs;

// Lots of unimportant code here

public void cancelClass() { obs.setChanged(); obs.notifyObservers(“Go ski”);}

}

setChanged has protected access

Page 17: Lecture 8: Java’s Observable & Observer

%&#*@ Awful Design

Page 18: Lecture 8: Java’s Observable & Observer

%&#*@ Awful Design

setChanged & clearChanged protected accessjava.util package is sealed; cannot write class for itCan only be called & used from within subclass

Must write subclass of Observable to use it

As this is Java, that class cannot extend others

Updates received in method of Observer:

void update(Observable o, Object arg)This really means that can only push 1 objectKills point of push model since cannot push much

Page 19: Lecture 8: Java’s Observable & Observer

%&#*@ Awful Design

setChanged & clearChanged protected accessjava.util package is sealed; cannot write class for itCan only be called & used from within subclass

Must write subclass of Observable to use it

As this is Java, that class cannot extend others

Updates received in method of Observer:

void update(Observable o, Object arg)This really means that can only push 1 objectKills point of push model since cannot push much

Useless

Page 20: Lecture 8: Java’s Observable & Observer

Final Attempt

public class CSCClass extends Observable {public void cancelClass() { setChanged(); notifyObservers(“Go ski”);}

}public class Student implements Observer {

public void update(Observable o, Object arg) { String s = (String)arg; if (s.equals(“Get here NOW!!!”)) // Go skiing else // Go to class; think about skiing}

}

Page 21: Lecture 8: Java’s Observable & Observer

Final Attempt

Page 22: Lecture 8: Java’s Observable & Observer

Final Attempt

Page 23: Lecture 8: Java’s Observable & Observer

Final Attempt

Page 24: Lecture 8: Java’s Observable & Observer

Final Attempt

Page 25: Lecture 8: Java’s Observable & Observer

Observable Pros & Cons

Cons:

Pros:

Page 26: Lecture 8: Java’s Observable & Observer

Observable Pros & Cons

Cons: Need a subclass of Observable to use Must call setChange before notifyObservers

Can push only 1 object to Observers Pros:

Already has code to add & remove Observers

Easy to rewrite and improve upon this design

Page 27: Lecture 8: Java’s Observable & Observer

For Next Lecture

Two (short) readings available on web Will start looking into how code is

optimized How does Java run & make programs

faster? What can we do to make our code

faster? Using final can be huge. Why would it

matter?

Lab #2 due in 1 week & available on Angel Make sure that you also check the

grading rubric Help available in WTC206 during the lab

time