View RSS Feed

fatabass

Using the Observable - Observer class , interface in JAVA. Observer Pattern example.

Rating: 2 votes, 2.00 average.
by , 03-03-2012 at 10:00 PM (8877 Views)
Java Code:
package observerPatternBuiltIn;

import java.util.Observable;

public class MyObservable extends Observable {
	
	int integerToBeObserved;
	
	public void setInt(int i) {
		integerToBeObserved = i;
		setChanged();
		notifyObservers();
	}
	
	public int getInt() {
		return integerToBeObserved;
	}

}


package observerPatternBuiltIn;

import java.util.Observable;
import java.util.Observer;

public class MyObserver implements Observer {

	
	
	@Override
	public void update(Observable obs, Object arg1) {
		System.out.println(((MyObservable) obs).getInt());
	}
}

package observerPatternBuiltIn;

public class TestClass {

	public static void main(String[] args) {
		
		MyObservable myObservableObject = new MyObservable();
		myObservableObject.addObserver(new MyObserver());
		myObservableObject.setInt(5);
		myObservableObject.setInt(15);
	}
}

Submit "Using the Observable - Observer class , interface in JAVA. Observer Pattern example." to Facebook Submit "Using the Observable - Observer class , interface in JAVA. Observer Pattern example." to Digg Submit "Using the Observable - Observer class , interface in JAVA. Observer Pattern example." to del.icio.us Submit "Using the Observable - Observer class , interface in JAVA. Observer Pattern example." to StumbleUpon Submit "Using the Observable - Observer class , interface in JAVA. Observer Pattern example." to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments