Observer Pattern - Note PDF
Observer Pattern - Note PDF
Observer Pattern
The Observer Pattern keeps your objects informed when something they might care
about happens. Objects can even decide at run time whether they want to be kept
informed. The Observer pattern is one of the most heavily used patterns in the JDK
or .NET FCL, and it is very useful. We will also look at one to many relationships and
loose coupling .
!
The EngineData object knows how to talk to the physical Engine, to get updated data.
The EngineData object then updates its displays for the three different display elements:
Analog Display (shows temperature, rpm, and speed), Digital Display, and a simple
gauge.
Project Requirements
1. Our job is to create an app that uses the EngineData object to update the three
displays for analog display, digital display, and a gauge. These displays must be
Page 1 of 10
Observer Pattern
Page 2 of 10
Observer Pattern
!
There are few different ways to implement the Observer Pattern but most revolve around
a class design that includes Subject and Observer Interfaces.
1. Subject interface : objects use this interface to register as observers and also to
remove themselves from being observers.
2. Observer interface : all potential observers need to implement the observer
interface. This interface just has one method, update() that gets called when the
subject's state changes.
3. Concrete subject ; implements the Subject interface. In addition to the register and
remove methods, the concrete subject implements a notifyObservers() method
that is used to update all the current observers whenever state changes.
4. The concrete subject may also have methods for setting and getting its state.
5. Concrete observers can be any class that implements the Observer interface.
Each observer registers with a concrete subject to receive updates.
Advantages
The subject is the sole owner of the data, the observers are dependent on the subject to
update them when the data changes. This leads to a good OO design than allowing
many objects to control the same data.
Page 3 of 10
Observer Pattern
Loose Coupling
When two objects are loosely coupled, they can interact, but have veryt little knowledge
of each other.
The Observer Pattern provides an object design where subjects and observers are
loosely coupled.
The Subject knows nothing about the observer except the Observer interface.
We can add new observers at any time. Likewise, we can remove observers at any
time.
We never need to modify the subject to add new types of observers.
We can reuse subjects or observers independently of each because they are not tightly
coupled.
Changes to either the subject or an observer will not affect the other.
Practice
Implement the Car Dashboard App, the EngineData class and its display elements
using Class Diagrams.
!
Page 4 of 10
Observer Pattern
Implementing the Dashboard
public interface Subject
{
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObserver();
}
public EngineData()
{
observers = new ArrayList<Observer>();
}
@Override
public void registerObserver(Observer o)
{
observers.add(o);
}
@Override
public void removeObserver(Observer o)
{
int i = observers.indexOf(o);
Page 5 of 10
Observer Pattern
if (i >= 0)
observers.remove(i);
}
@Override
public void notifyObserver()
{
for (int i = 0; i < observers.size(); i++)
{
Observer observer = (Observer) observers.get(i);
observer.update(temperature, rpm, speed);
}
}
Building the display element
public class AnalogDisplay implements Observer, DisplayElement
{
private float temperature;
private float rpm;
private Subject engineData;
@Override
public void display()
{
System.out.println("Analog Display: "
+ temperature + "F degrees and "
Page 6 of 10
Observer Pattern
+ rpm + " revolutions per minute");
}
@Override
public void update(float temp, float rpm, float speed)
{
this.temperature = temp;
this.rpm = rpm;
display();
}
}
Power up your car
public class Dashboard
{
public static void main(String[] args)
{
EngineData engineData = new EngineData();
OO Principles
1.
2.
3.
4.
Page 7 of 10
Observer Pattern
Observer interface
Aggregation
Observabke class
update()
addObservers()
delefeObservers()
Inheritance
notifyObservers()
Inheritance
setChanged()
Inheritance
CourseData class
Dean class
Professor class
update()
update()
display()
display()
getAttendance()
getTestScores()
CourseData.java
import java.util.Observable;
Page 8 of 10
Observer Pattern
Displayable.java
public interface Displayable {
public void display();
}
Dean.java
import java.util.Observable;
import java.util.Observer;
Page 9 of 10
Observer Pattern
Professor.java
import java.util.Observable;
import java.util.Observer;
Dashboard.java
public class Dashboard {
Page 10 of 10