👂
🦻
👂
👂
🎶🐦
An interesting thing tells interested objects that something cool happened.
SO much coupling!
Strategies?
Interesting
Thing
Interested
Class2
Interested
Class3
Interested
Class1
1
*
1
*
1
*
We make mistakes
We identify our mistakes
We learn from our mistakes
We prevent mistakes
We Evolve Design Patterns
Lessons Learned!
I want arbitrarily many objects to know about a state without knowing anything about these objects.
I want these objects to be added and removed as desired.
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/
original image source: https://refactoring.guru/ (right image edited)
Either the customer wastes time checking product availability or the store wastes resources notifying the wrong customers.
Customer waiting for new product
Would you want to be notified about ALL tweets?
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);
}
}
The particular names here (but especially "Concrete"!) will change.
Observer
an Observer
Newspaper kid 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 kid 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
0..*
Recommended Additional Reading:
https://refactoring.guru/design-patterns/observer