It’s a behavioral pattern and
can be understood as publish-subscribe model. An object notifies to other
objects about his state changes.
“This pattern talks about
one-to-many dependency b/w objects to provide a behavior where change of one
object state would be notified to other objects and those objects would be
updated accordingly as well.”
An object which is being
watched known as ‘Subject’. An object which is watching the state is known as
‘Observer’ or ‘Listener’.
Example:
1) Using my own
implementation as below:
Observer.java
interface Observer {
void update(Object newsContent);
}
Subject.java
import java.util.ArrayList;
public abstract class Subject
{
protected ArrayList<Observer> observers = new
ArrayList<Observer>();
public
void registerObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public abstract void notifyObservers();
public void notifyObservers(Object obj){
for (Observer ob : observers) {
System.out.println("Notifying
Observers on change...");
ob.update(obj);
}
}
}
News.java
public class News extends
Subject {
private
String newsContent;
private String advertisement;
private int noOfPages;
public News(String newsContent, String advertisement,
int noOfPages) {
this.newsContent = newsContent;
this.advertisement =
advertisement;
this.noOfPages = noOfPages;
}
@Override
public void notifyObservers() {
for (Observer ob : observers) {
System.out.println("Notifying
Observers on change...");
ob.update(this.newsContent);
}
}
public String getNewsContent() {
return newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
notifyObservers();
}
public String getAdvertisement() {
return advertisement;
}
public void setAdvertisement(String advertisement) {
this.advertisement = advertisement;
notifyObservers(this.advertisement);
}
public int getNoOfPages() {
return noOfPages;
}
public void setNoOfPages(int noOfPages) {
this.noOfPages = noOfPages;
}
}
ElectronicNewsPaper.java
public class
ElectronicNewsPaper implements Observer {
@Override
public void update(Object newsContent) {
System.out.println("ElectronicNewsPaper
Today's News Content : " + (String)newsContent);
//Business-logic to update the
Electronic News-Paper
}
}
PrintNewsPaper.java
public class PrintNewsPaper
implements Observer {
@Override
public void update(Object newsContent) {
System.out.println("PrintNewsPaper
Today's News Content : " + (String)newsContent);
//Business-logic to update the Print
News-Paper
}
}
ObserverClient.java
public class ObserverClient {
public static void main(String args[]) {
PrintNewsPaper printMedia = new
PrintNewsPaper();
ElectronicNewsPaper electronicMedia =
new ElectronicNewsPaper();
News todayNews = new News("today's
News........................", "today's
Advertisement...........s", 12);
todayNews.registerObserver(printMedia);
todayNews.registerObserver(electronicMedia);
todayNews.setNewsContent("today's
News got updated...................");
System.out.println("=================================================");
todayNews.removeObserver(electronicMedia);
todayNews.setNewsContent("today's
News again got updated...................");
todayNews.setAdvertisement("today's
Advertisement got updated...................");
}
}
Output:-
Notifying Observers on
change...
PrintNewsPaper Today's News
Content : today's News got updated...................
Notifying Observers on
change...
ElectronicNewsPaper Today's
News Content : today's News got updated...................
===================================================
Notifying Observers on
change...
PrintNewsPaper Today's News
Content : today's News again got updated...................
Notifying Observers on
change...
PrintNewsPaper Today's News
Content : today's Advertisement got updated...................
2) Using Java built-in API as below:
The
java API has one Interface called as ‘java.util.Observer’ and one class called
as ‘java.util.Observable’ to provide pub-sub mechanism.
News.java
import java.util.Observable;
public class News extends
Observable{
private String newsContent;
private String advertisement;
private int noOfPages;
public News(String newsContent, String advertisement,
int noOfPages) {
this.newsContent = newsContent;
this.advertisement =
advertisement;
this.noOfPages = noOfPages;
}
public String getNewsContent() {
return newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
setChanged();
notifyObservers(newsContent);
}
public String getAdvertisement() {
return advertisement;
}
public void setAdvertisement(String advertisement) {
this.advertisement = advertisement;
}
public int getNoOfPages() {
return noOfPages;
}
public void setNoOfPages(int noOfPages) {
this.noOfPages = noOfPages;
}
}
ElectronicNewsPaper.java
import java.util.Observable;
import java.util.Observer;
public class
ElectronicNewsPaper implements Observer {
@Override
public void update(Observable arg0, Object
newsContent) {
System.out.println("ElectronicNewsPaper
Today's News Content : " + (String)newsContent);
//Business-logic to update the
Electronic News-Paper
}
}
PrintNewsPaper.java
import java.util.Observable;
import java.util.Observer;
public class PrintNewsPaper
implements Observer {
@Override
public void update(Observable arg0, Object
newsContent) {
System.out.println("PrintNewsPaper
Today's News Content : " + (String)newsContent);
//Business-logic to update the Print
News-Paper
}
}
ObserverClient.java
public class ObserverClient {
public static void main(String args[]) {
PrintNewsPaper printMedia = new
PrintNewsPaper();
ElectronicNewsPaper electronicMedia =
new ElectronicNewsPaper();
News todayNews = new News("today's
News........................", "today's
Advertisement...........s", 12);
todayNews.addObserver(printMedia);
todayNews.addObserver(electronicMedia);
todayNews.setNewsContent("today's
News got updated...................");
System.out.println("===================================================");
todayNews.deleteObserver(electronicMedia);
todayNews.setNewsContent("today's
News again got updated...................");
}
}
Output:-
ElectronicNewsPaper Today's
News Content : today's News got updated...................
PrintNewsPaper Today's News
Content : today's News got updated...................
===================================================
PrintNewsPaper Today's News
Content : today's News again got updated...................