View RSS Feed

fatabass

Self Made Observable Pattern

Rating: 1 votes, 1.00 average.
by , 03-03-2012 at 10:50 PM (748 Views)
Java Code:
package observerPatternSelfMade;

public class MyObservableClass implements MyObservableInterface {

	
	
	@Override
	public void registerObserver(MyObserverInterface o) {
		observers.add(o);
	}

	@Override
	public void removeObserver(MyObserverInterface o) {
		observers.remove(o);
	}

	@Override
	public void notifyObservers() {
		
		for(int i = 0; i<observers.size();i++)
		{
			MyObserverInterface obj = observers.get(i);
			obj.update();
		}
	}

}


package observerPatternSelfMade;

import java.util.ArrayList;

public interface MyObservableInterface {
	
	void registerObserver(MyObserverInterface o);
	
	void removeObserver(MyObserverInterface o);
	
	void notifyObservers();
	
	ArrayList<MyObserverInterface> observers = new ArrayList<MyObserverInterface>();

}

package observerPatternSelfMade;

public class MyObserverClass implements MyObserverInterface {

	@Override
	public void update() {
		System.out.println("I have been updated.");
	}
}


package observerPatternSelfMade;

public interface MyObserverInterface {
	
	void update();

}

package observerPatternSelfMade;

public class TestClass {
	
	public static void main(String[] args)
	{
		MyObservableInterface myObserverableObj = new MyObservableClass();
		myObserverableObj.registerObserver(new MyObserverClass());
		myObserverableObj.notifyObservers();
	}
}

Submit "Self Made Observable Pattern" to Facebook Submit "Self Made Observable Pattern" to Digg Submit "Self Made Observable Pattern" to del.icio.us Submit "Self Made Observable Pattern" to StumbleUpon Submit "Self Made Observable Pattern" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments