Slides designed by Felix Grund, based on Paul Carter’s slides
Use a private browser window if you want to remain anonymous.
I hope you are all well given the current circumstances!
We make mistakes
We identify our mistakes
We learn from our mistakes
We prevent mistakes
We Evolve Design Patterns
Lessons Learned!
Image source: https://refactoring.guru/
I want arbitrarily many objects to know about a state but I don't know what these objects are exactly.
I want that these objects can be added and removed as they desire.
Intent: To define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
The observer pattern allows us to achieve the notification without introducing coupling between the subject and its dependents.
Image source: https://refactoring.guru/
...or store wastes resources notifying the wrong customers.
Customer waiting for new product...
...either customer wastes time checking product availability...
Would you want to be notified about ALL tweets?
X / Twitter BlueSky 🦋
Image source: https://codepumpkin.com/observer-design-pattern/
private class DrawingMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
handleMousePressed(translateEvent(e));
}
public void mouseReleased(MouseEvent e) {
handleMouseReleased(translateEvent(e));
}
public void mouseClicked(MouseEvent e) {
handleMouseClicked(translateEvent(e));
}
public void mouseDragged(MouseEvent e) {
handleMouseDragged(translateEvent(e));
}
private MouseEvent translateEvent(MouseEvent e) {
return SwingUtilities.convertMouseEvent(
e.getComponent(), e, currentDrawing);
}
}
Newspaper boy delivers you the newspapers. Why?
Because you registered!
Newspaper reaches the shop → newspaper boy delivers it to all registered customers
Imagine you don't like this newspaper anymore
You unregister!
Newspaper boy doesn't come anymore
Observers register with Observable/Subject for getting information about the occurrences of events
Roles: the observer pattern requires us to identify:
FavoriteColorData
void setData(Color c, int numVotes)
PieChart
void update(Color c, int numVotes)
JFreeChart getChart()
MaxVotesPanel
void update(Color c, int numVotes)
0..*
FavoriteColorData
void setData(Color c, int numVotes)
void addObserver(Observer o)
void notifyObservers(Color c, int numVotes)
Observer
void update(Color c, int numVotes)
<<interface>>
MaxVotesPanel
void update(Color c, int numVotes)
PieChart
void update(Color c, int numVotes)
JFreeChart getChart()
0..*
Observable
void addObserver(Observer o)
void notifyObservers(Color c, int numVotes)
FavoriteColorData
void setData(Color c, int numVotes)
Observer
void update(Color c, int numVotes)
<<interface>>
MaxVotesPanel
void update(Color c, int numVotes)
PieChart
void update(Color c, int numVotes)
JFreeChart getChart()
Observable
MessagePrinter
Observer
addObserver(Observer o)
notifyObservers()
update()
= Observable
Observer
Observer
Observable
Event
(Nothing)
Observer
Observer
Observable
Event
Observer
Observer
Observable
Event