Skip to content

Instantly share code, notes, and snippets.

@CaglarGonul
Created April 8, 2013 13:20
Show Gist options
  • Save CaglarGonul/5336713 to your computer and use it in GitHub Desktop.
Save CaglarGonul/5336713 to your computer and use it in GitHub Desktop.
A built in observer pattern. It seems it is no different than my callback implementation in Java
package com.chp2.observerpattern.custom;
/**
* The objects who will broadcast events will implement this interface
*/
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
package com.chp2.observerpattern.custom;
/**
* The objects who will be subscribers to the broadcaster will implement this interface.
*/
public interface Observer {
public void update(float temp,float humidity,float pressure);
}
package com.chp2.observerpattern.custom;
/**
* All 3 displays will implement this interface
*/
public interface DisplayElement {
public void display();
}
package com.chp2.observerpattern.custom;
import java.util.ArrayList;
/**
* This class is the broadcaster
*/
public class WheatherData implements Subject{
private ArrayList<Observer> observers;
private float temperature;
private float hummidity;
private float pressure;
public WheatherData(){
observers = new ArrayList<>();
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
if(observers.contains(o)){
observers.remove(o);
}
}
@Override
public void notifyObservers() {
for (Observer b_Observer : observers) {
b_Observer.update(temperature, hummidity, pressure);
}
}
public void measurementsChanged(){
notifyObservers();
}
public void setMeasurements(float temp,float hum,float pres){
this.temperature = temp;
this.hummidity = hum;
this.pressure = pres;
measurementsChanged();
}
}
package com.chp2.observerpattern.custom;
public class CurrentConditionsDisplay implements Observer, DisplayElement{
private float temperature;
private float humidity;
private Subject whetherData;
public void registerObserver(Subject wd){
this.whetherData = wd;
this.whetherData.registerObserver(this);
}
@Override
public void update(float temp, float humidity, float pressure) {
this.temperature = temp;
this.humidity = humidity;
display();
}
@Override
public void display() {
StringBuilder sb = new StringBuilder();
sb.append("Current conditions : " ).append(temperature).append(" F degrees and hummidity ").append(humidity);
System.out.println(sb.toString());
}
}
@Test
public void test_observer(){
WheatherData wd = new WheatherData();
CurrentConditionsDisplay cd = new CurrentConditionsDisplay();
cd.registerObserver(wd);
wd.setMeasurements(80, 65, 30.4f);
wd.setMeasurements(82, 70, 29.2f);
wd.setMeasurements(78, 90, 29.2f);
}
Current conditions : 80.0 F degrees and hummidity 65.0
Current conditions : 82.0 F degrees and hummidity 70.0
Current conditions : 78.0 F degrees and hummidity 90.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment