You are on page 1of 2

design pattern

add feature
1-inheritance with overriding
2-interface

//subject interface
public interface Subject{

protected void registerObserver(Observer o);


protected void removeObserver(Observer o);
protected void notifyObserver();
}

//subject conceret
public class WheatherData implements Subject{

//data
private float tempeature;
private float humidity;
private float pressure;
private Array Observers;

//constructor
public WheatherObject(){
Observers = new Array();
}
//public interface
protected void registerObserver(Observer o){
Observers.add(o);
}
protected void removeObserver(Observer o){
int i = Observers.indexOf();
if(i>=){
Observers.remove(i);
}
}
protected void notifyObserver(){
for(int i=0; i<=Observers.size(); i++){
Observer observer = Observers.get(i);
observer.update(tempeature, humidity, pressure);
}
}
public void measurementChanged(){
notifyObserver();
}
public void setMeasurement(float temp, float humi, float pres){
this.tempeature = temp;
this.humidity = humi;
this.pressure = pres;
measurementChanged();
}
}
//observer interface
public interface Observer{ public void update(float temp, float humi, float
pres); }

//observer concerets implementions


public class CurrentCondition implements Observer{
//data
private float tempeature;
private float humidity;
private float pressure;
private Subject weatherData;
//message from subject
public void update(float temp, float humi, float pres){
this.tempeature = temp;
this.humi = humidity;
this.pressure = pres;
}
public void display(){
System.out.println();
}
}

public class Statistic implements Observer{


//data
private float minTemp;
private float maxTemp;
private float avgTemp;
private int row;
private Subject weatherData;
//message from subject
public void update(float temp, float humi, float pres){
this.tempeature = temp;
this.humi = humidity;
this.pressure = pres;
}
public void display(){
System.out.println();
}
}

public class Future implements Observer{


//data
private float tempeature;
private float humidity;
private float pressure;
private Subject weatherData;
//message from subject
public void update(float temp, float humi, float pres){
this.tempeature = temp;
this.humi = humidity;
this.pressure = pres;
}
public void display(){
System.out.println();
}
}

You might also like