UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Observer Pattern in Java.

Somenath Mukhopadhyay
22/03/2017 0 0

As I was going through the different source code files of the Java Util package, I found the implementation of the Observer Pattern ( see the Gang of Four books for more information on this pattern) in the two places - namely Observer.java and Observable.java. I would like to throw some lights on these two classes. These two classes can be found in the Java\j2se\src\share\classes\java\util folder of the JDK source code.

But first of all, I need to give you a practical example of why we need observer pattern at the first place 

Suppose, we have a document which can be viewed simultaneously by three different views – say one view represents a line graph chart, another view represents it in a spreadsheet, yet another view represents a pie chart. Now suppose the spreadsheet view makes some modification to the document. If the other two views don't update themselves with this changed state of the document, different views will be in inconsistent states. So we need some mechanism to notify the other two views whenever the spreadsheet view updates the document. This is done through Observer pattern in which whenever the document changes state, it notifies all of its views. The views, in turn, updates themselves with the latest data.

So now let us first start with the Observable.java class. As the name suggests it is the class which will implement functionalities for being observed. Or in other words, it is the class which helps in designing the Subject class of the Observer pattern discussion of the GoF book. We need to extend this class to get the Subject class.

Let us try to dissect this class. The following functions are there in this class: 

  1. Data Members: It has a vector to hold all the observers that are interested in observing this observable class. It has another boolean data member called "changed" to indicate if anything has changed in the Subject class ( which will be derived from this Observable class). 
  2. Constructor: This class has a no-argument constructor to construct an empty vector of Observers.
  3. Member Functions:
  • AddObserver: To add an observer to its list of Observers.
  • DeleteObserver: To delete a particular observer from the list of the observers.
  • NotifyObservers: There are two overloaded versions of this function. One takes an Object parameter as an argument and the other does not take any argument. The task of this function is to notify all the attached observers when any data of the subject gets changed. To check whether the data is changed it evaluates the boolean "changed" data member. This function also calls the update function of each observer objects to ask them to get in sync with the subject's changed state. The overloaded version that takes a one argument parameter is used to let the observers know which attribute is changed. And the other version of this function which does not take any argument does not let the observer know about which attribute is changed.
  • DeleteObservers: This function removes all the observers attached to this subject.
  • SetChanged: This function sets the boolean data member "changed".
  • ClearChanged: This function resets the boolean data member "changed".
  • HasChanged: This function helps us to know whether the data of the subject has been changed or not.
  • CountObservers: This function returns the number of observers attached to this subject.

This is all about the Observable class which helps us to define to Subject class.

The Observer.java defines an interface called Observer having just one abstract function called update (Observable o, Object arg). As the name suggests, the Observer class that will implement this interface will override the update function to set the attribute passed as an argument (arg) from the Subject class. This method is called whenever any attribute in the Subject class gets changed.

Now let us try to see an example to understand how this Observer Pattern is used. Let us first extend the Observable class to create the Subject class.

package com.somitsolutions.training.java.observerpattern. 

import java.util.Observable;

public class Subject extends Observable {

private String name;

private float price;

public Subject(String name, float price) {

this.name = name;

this.price = price;

}

public String getName() {

return name;

}

public float getPrice() {

return price;

}

public void setName(String name) {

this.name = name;

setChanged();

notifyObservers(name);

}

public void setPrice(float price) {

this.price = price;

setChanged();

notifyObservers(new Float(price));

}

}

As this is clear from the implementation of the Subject class, that whenever we call the setter function to change the attributes of the Subject's object, we call the notifyObservers and pass that attribute as a parameter.

Now let us see how we create two different observers namely NameObserver and PriceObserver to observe these two attributes of the Subject class.

// An observer of name changes.

package com.somitsolutions.training.java.observerpattern;

import java.util.Observable;

import java.util.Observer; 

public class NameObserver implements Observer {

private String name;

public NameObserver() {

name = null;

System.out.println("NameObserver created: Name is " + name);

}

@Override

public void update(Observable o, Object arg) {

// TODO Auto-generated method stub

if (arg instanceof String) {

name = (String)arg;

System.out.println("NameObserver: Name changed to " + name);

}

}

}

// An observer of price changes.

package com.somitsolutions.training.java.observerpattern;

import java.util.Observable;

import java.util.Observer; 

public class PriceObserver implements Observer {

private float price; public PriceObserver() {

price = 0;

System.out.println("PriceObserver created: Price is " + price);

}

@Override

public void update(Observable o, Object arg) {

// TODO Auto-generated method stub

if (arg instanceof Float) {

price = ((

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Fractional Knapsack
Algorithm - Fractional Knapsack import java.util.Scanner; public class Knapsack01 { public static void main(String args) { Scanner scan = new Scanner(System.in); int...

Java Achieve (JAR)
Java Archieve is a binary form to bundle a Java library which equivalent to zip file. It contains .class files, .properties, .jpeg, OR any supporting files to execute this library. For Example- junit.jar,...

Priority in TestNG
public class Priority { @Test (priority=1)public void login() {System.out.println("login");} @Testpublic void email1() {System.out.println("email1");} @Test (priority=-2)public void email2() {System.out.println("email2");} //I...
S

Sarthak C.

0 0
0

Java :: Inner Class Concepts
Java:: Inner Class Concepts--------------------------------- Inner class concepts introduced in Java v1.1 - as a part of AWT Event Handling improvements.When to use Inner class: - Without existing One...
M

Marimuthu P.

2 0
0

What Is Applet & Its Life Cycle?
What is Applet & its life cycle?Applet is a Java programme that can be embedded into HTML page.Java Applet runs on the java enables web browsers such as Mozilla & Internet Explorer.Applets are...
I

ICreative Solution

0 0
0
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more