观察者模式(温度,压力和湿度)

时间:2014-09-26 11:06:27

标签: java

我需要你以我的观察者模式提供帮助,我想在更新TEMPERATURE或PRESSURE或HUMIDITY时更新。这是代码:

public static void main(String[] args) {
    TemperatureSensor t = new TemperatureSensor();
    PressureSensor p = new PressureSensor();
    HumiditySensor h = new HumiditySensor();
    WeatherObserver w = new WeatherObserver();
    w.addSubject(t);
    w.addSubject(p);
    w.addSubject(h);
    t.setTemperature(28);
    System.out.println("-------------------------------");
    p.setPressure(200);
    System.out.println("-------------------------------");
    h.setHumidity(89);
}

class Subject {
    private List<Observer> observers=new ArrayList<Observer>();
    public void attachObserver(Observer observer) {
    this.observers.add(observer);
    }
    public void detachObserver(Observer observer) {
    this.observers.remove(observer);
    }
    public void notifyObservers() {
    for (Observer observer: this.observers)
            observer.update(this);
    }
}

class TemperatureSensor extends Subject {
    private double temperature;
    public double getTemperature() { return this.temperature; }    
    public void setTemperature(double temp) {
    this.temperature=temp;         
       this.notifyObservers();  // don't forget to do this!
    }
}

class PressureSensor extends Subject {
    private double pressure;
    public double getPressure() { return this.pressure; }    
    public void setPressure(double press) {
    this.pressure=press;         
    this.notifyObservers();  // don't forget to do this!
    }
}

class HumiditySensor extends Subject {
    private double humidity;
    public double getHumidity() { return this.humidity; }    
    public void setHumidity(double humid) {
    this.humidity=humid;         
        this.notifyObservers();  // don't forget to do this!
    }
}

abstract class Observer {
    public Observer(Subject subject) {
    subject.attachObserver(this);
    }
    abstract public void update(Subject subject);
}

class WeatherObserver extends Observer {
    public WeatherObserver(Subject subject) {
    super(subject);
    }

    public void addSubject(Subject subject){
        subject.attachObserver(this);
    }

    public void update(Subject subject) {
        TemperatureSensor temp=(TemperatureSensor)subject;        
        System.out.println("Temperature: " + temp.getTemperature() + " Celcius");
        /*PressureSensor press=(PressureSensor)subject;
        System.out.println("the temp is: " + press.getPressure() + " Pascal");
        HumiditySensor humid=(HumiditySensor)subject;
        System.out.println("the temp is: " + humid.getHumidity());*/
    }    
}

如何在更新方法中更新压力和湿度?无论如何还有多个科目?请指导我这个。

2 个答案:

答案 0 :(得分:0)

基本上你正在制作新的观察者名单是主题,这应该是所有人共同的。 那是错误的。

public static void main(String[] args) {
    List<Observer> observers = new ArrayList<Observer>();
    TemperatureSensor t = new TemperatureSensor(observers);
    PressureSensor p = new PressureSensor(observers);
    HumiditySensor h = new HumiditySensor(observers);
    WeatherObserver w = new WeatherObserver();
    w.addSubject(t);
    w.addSubject(p);
    w.addSubject(h);
    t.setTemperature(28);
    System.out.println("-------------------------------");
    p.setPressure(200);
    System.out.println("-------------------------------");
    h.setHumidity(89);
}

class Subject {
protected List<Observer> observers;

public Subject(List<Observer> observers) {
    this.observers = observers; // you need common observer so that you can
                                // receive callback
}

public void attachObserver(Observer observer) {
    this.observers.add(observer);
}

public void detachObserver(Observer observer) {
    this.observers.remove(observer);
}

public void notifyObservers() {
    for (Observer observer : this.observers)
        observer.update(this);
}
}

class TemperatureSensor extends Subject {

public TemperatureSensor(List<Observer> observers) {
    super(observers);
}
private double temperature;

public double getTemperature() {
    return this.temperature;
}

public void setTemperature(double temp) {
    this.temperature = temp;
    this.notifyObservers(); // don't forget to do this!
}
}

class PressureSensor extends Subject {
public PressureSensor(List<Observer> observers) {
    super(observers);
}
private double pressure;

public double getPressure() {
    return this.pressure;
}

public void setPressure(double press) {
    this.pressure = press;
    this.notifyObservers(); // don't forget to do this!
}
}

class HumiditySensor extends Subject {

public HumiditySensor(List<Observer> observers) {
    super(observers);
}
private double humidity;

public double getHumidity() {
    return this.humidity;
}

public void setHumidity(double humid) {
    this.humidity = humid;
    this.notifyObservers(); // don't forget to do this!
}
}

abstract class Observer {
// public Observer(Subject subject) {
// subject.attachObserver(this);
// }

abstract public void update(Subject subject);
}

class WeatherObserver extends Observer {
// public WeatherObserver(Subject subject) {
// super(subject);
// }

public void addSubject(Subject subject) {
    subject.attachObserver(this);
}

public void update(Subject subject) {
    System.out.println("updated");
    // TemperatureSensor temp = (TemperatureSensor) subject;
    // System.out.println("Temperature: " + temp.getTemperature() + " Celcius");
    /*
     * PressureSensor press=(PressureSensor)subject;
     * System.out.println("the temp is: " + press.getPressure() +
     * " Pascal"); HumiditySensor humid=(HumiditySensor)subject;
     * System.out.println("the temp is: " + humid.getHumidity());
     */
}
}

希望这能解决您的问题

答案 1 :(得分:0)

您只需要知道在更新方法中接收的对象类型,为此您可以使用关键字instanceof。

class WeatherObserver extends Observer {
   public WeatherObserver(Subject subject) {
      super(subject);
   }

   public void addSubject(Subject subject){
      subject.attachObserver(this);
   }

   @Override
   public void update(Subject subject) {
      if (subject instaceof TemperatureSensor){
          TemperatureSensor temp=(TemperatureSensor)subject;        
          System.out.println("Temperature: " + temp.getTemperature() + " Celcius");
      } else if (subject instaceof PressureSensor ){
          PressureSensor press=(PressureSensor)subject;
          // use press
      } else if (subject instaceof HumiditySensor ){
          HumiditySensor humid=(HumiditySensor)subject;
          // use humid
      } else {
          throw new ClassCastExeption("Unexpected instance of Subject: " + subject.getClass());
      }
    }    
 }