Created
April 8, 2013 13:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chp2.observerpattern.custom; | |
/** | |
* All 3 displays will implement this interface | |
*/ | |
public interface DisplayElement { | |
public void display(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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