[ Update method of Observer Design Pattern ]
I have seen few examples of Observer pattern.
Why does the update method in the Observer interface in some cases include a reference to the object being observed? Doesn't the observer know what object it is observing?
Please explain with example.
Answer 1
It may or may not have a reference to the Subject, depending on the concrete problem.
The Subject might only call the update() function and just set some value in the Observer, thus the Observer does not need a reference. Or it can notify the Observer that there has been a change, and the Observer will contact the Subject by the reference it has and get the new values.
Holding the reference in the implementation can also be used if the Observers have need to inform the Subject about something.
Answer 2
Specifically to your last question, given C# and Java as programming languages, how would one object "know" about another object, except via a reference?
The relationship between an observer and a subject needs to be stored somewhere, and in Java and C# can only be stored via references, if any actions need to be invoked on either the subject or the observer.
Answer 3
Hello Amandeep you can read this article it's very interessant, you have all design patterns of GOF
You have class diagram, sample code
It explain all concepts about observation : Subject, ConcreteSubject, Observer and ConcreteObserver
http://www.dofactory.com/Patterns/PatternObserver.aspx
Answer 4
One Observer
can observe many Observable
so when he gets notified it can handle the change in a specific manner. For example :
import java.util.Observable;
import java.util.Observer;
public class ObserverSample {
static class ObservableExtension extends Observable {
void doSomething() {
setChanged();
notifyObservers();
clearChanged();
}
}
public static void main(final String[] args) {
final ObservableExtension observable1 = new ObservableExtension();
final Observable observable2 = new ObservableExtension();
final Observer observer = new Observer() {
@Override
public void update(final Observable o, final Object arg) {
if (o == observable1)
System.out.println("This is the first observable");
else if (o == observable1)
System.out.println("This is the second observable");
else
System.out.println("This is another observable");
}
};
observable1.addObserver(observer);
observable2.addObserver(observer);
observable1.doSomething();
}
}
However, i don't like this approach ( The sole advantage is a smaller memory footprint.), I prefer having different Observer with a single responsability.
observable1.addObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
System.out.println("This is the first observable");
}
});
observable2.addObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
System.out.println("This is the second observable");
}
});
Answer 5
Quick Short Answer
There can be several variations of the Observer Pattern.
One of them allows only to detect changes of the observed objects, and maybe display their properties values. Other allows to perform an operation also.