阅读下列说明和C++代码,回答问题,将答案填入相应横线处。【说明】某实验室欲建立一个实验室环境监测系统,能够显示实验室的温度、湿度以及洁净度等环境数据。当获取到最新的环境测量数据时,显示的环境数据能够更新现在采用观察者(observer)模式来开发该系统,观察者模式的类图如下图所示。 ?【C++代码】#include<iostream>#include<vector>usingnamespacestd;classObserver{public:????virtualvoidupdate(floattemp,floathumidity,floatcleanness)=0;};classSubject{public:????virtualvoidregisterObserver(Observer*o)=0;//注册对主题感兴趣的观察者????virtualvoidremoveObserver(Observer*o)=0;//删除观察者????virtualvoidnotifyObservers()=0;//当主题发生变化时通知观察者};classEnvironmentData:public?????(1)????{private:vector<Observer*>observers;floattemperature,humidity,cleanness;public:????voidregisterObserver(Observer*o){observers.push_back(o);}????voidremoveObserver(Observer*o){/*代码省略*/}????voidnotifyObservers(){for(vector<Observer*>::const_iteratorit=observers.begin();???????????it!=observers.end();it++){???????(2)???;}}VoidmeasurementsChanged(){?????(3)???;}voidsetMeasurements(floattemperature,floathumidity,floatcleanness){????this->temperature=temperature;????this->humidity=humidity;????this->cleanness=cleanness;????????(4)???;????}};classCurrentConditionsDisplay:public?????(5)???{private:?????floattemperature,humidity,cleanness;????Subject*envData;public:????CurrentConditionsDisplay(Subject*envData){????????this->envData=envData;???????????(6)??;??????}voidupdate(floattemperature,floathumidity,floatcleanness)?{this->temperature=temperature;????this->humidity=humidity;????this->cleanness=cleanness;????display();}voiddisplay(){/*代码省略*/}};intmain(){????EnvironmentData*envData=newEnvironmentData();????CurrentConditionsDisplay*currentDisplay=newCurrentConditionsDisplay(envData);????envData->setMeasurements(80,65,30.4f);????return0;}
1、Subject[解析] 当主题中的环境发生变化,也就是调用measure-mentsChanged(),会通知观察者,即调用notifyObservers(),在notifyObservers()方法中,观查者会调用自身的up-date(float temperature,float humidity,float cleanness)。在主题的registerObserver(Observe*o)中会注册对主题感兴趣的观察者。 2、(*it)->update(temperature,humidity,clean-ness) 3、notifyObservers() 4、measurementsChanged() 5、Observer() 6、this->envData->registerObserver(this)