The observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.
-----------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Observable;
import java.util.Observer;
public class ObserverDesignPattern {
public static void main(String[] args) {
System.out.println("Enter Text >");
// create an event source - reads from stdin
final EventSource evSrc = new EventSource();
// create an observer
final ResponseHandler respHandler = new ResponseHandler();
// subscribe the observer to the event source
evSrc.addObserver(respHandler);
// starts the event thread
Thread thread = new Thread(evSrc);
thread.start();
}
}
class EventSource extends Observable implements Runnable {
public void run() {
try
{
final InputStreamReader isr = new InputStreamReader(System.in);
final BufferedReader br = new BufferedReader(isr);
while (true)
{
String response = br.readLine();
setChanged();
notifyObservers(response);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void notifyObservers(Object arg) {
// TODO Auto-generated method stub
//super.notifyObservers(arg);
System.out.println(" Customised Response :" + arg.toString());
}
}
class ResponseHandler implements Observer {
private String resp;
public void update(Observable obj, Object arg) {
if (arg instanceof String)
{
resp = (String) arg;
System.out.println("\nReceived Response: " + resp);
}
}
}