标签:col ali nts static 设计 res family 2.0 ace
public interface Subject { void registerObserver(Observer observer); void removeObserver(Observer observer); void notifyObservers(); }
public class WeatherDataSubject implements Subject{ List<Observer> observers; private float temp; private float humidity; private float pressure; public WeatherDataSubject() { observers = new ArrayList<>(); } public void registerObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } public void notifyObservers() { for(Observer observer:observers) { observer.update(temp, humidity , pressure); } } public void measurementsChanaged() { notifyObservers(); } public void setMeasurements(float temp, float humidity , float pressure) { this.temp = temp; this.humidity = humidity; this.pressure = pressure; measurementsChanaged(); } }
public interface Observer { void update(float temp,float humidity ,float pressure); }
public interface DisplayElement { void display(); }
public class CurrentConditionsDisplay implements Observer,DisplayElement { private float temp; private float humidity; private float pressure; @Override public void display() { System.out.println("CurrentConditionsDisplay:temp="+temp+";humidity="+humidity+";pressure="+pressure); } @Override public void update(float temp, float humidity, float pressure) { this.temp = temp; this.humidity = humidity; this.pressure = pressure; display(); } } public class StatisticsDisplay implements Observer,DisplayElement { private float temp; private float humidity; private float pressure; @Override public void display() { System.out.println("StatisticsDisplay:temp="+temp+";humidity="+humidity+";pressure="+pressure); } @Override public void update(float temp, float humidity, float pressure) { this.temp = temp; this.humidity = humidity; this.pressure = pressure; display(); } } public class ThirdPartyDisplay implements Observer,DisplayElement { private float temp; private float humidity; private float pressure; @Override public void display() { System.out.println("ThirdPartyDisplay:temp="+temp+";humidity="+humidity+";pressure="+pressure); } @Override public void update(float temp, float humidity, float pressure) { this.temp = temp; this.humidity = humidity; this.pressure = pressure; display(); } }
public class Test { public static void main(String[] args) { //观察者 CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(); ThirdPartyDisplay thirdPartyDisplay = new ThirdPartyDisplay(); //主题 WeatherDataSubject subject = new WeatherDataSubject(); //把观察者注册到主题中 subject.registerObserver(currentConditionsDisplay); subject.registerObserver(statisticsDisplay); subject.registerObserver(thirdPartyDisplay); subject.setMeasurements(1,1,1); subject.setMeasurements(2,3,4); } }
输出结果:
CurrentConditionsDisplay:temp=1.0;humidity=1.0;pressure=1.0
StatisticsDisplay:temp=1.0;humidity=1.0;pressure=1.0
ThirdPartyDisplay:temp=1.0;humidity=1.0;pressure=1.0
CurrentConditionsDisplay:temp=2.0;humidity=3.0;pressure=4.0
StatisticsDisplay:temp=2.0;humidity=3.0;pressure=4.0
ThirdPartyDisplay:temp=2.0;humidity=3.0;pressure=4.0
标签:col ali nts static 设计 res family 2.0 ace
原文地址:https://www.cnblogs.com/use-D/p/9748761.html