导航
您当前的位置:首页 > 计算机 > 软件水平
问题:

[问答题] 某实验室欲建立一个实验室环境监测系统,能够显示实验室的温度、湿度以及洁净度等环境数据。当获取到最新的环境测量数据时,显示的环境数据能够更新。现在采用观察者(Observer)模式来开发该系统。观察者模式的类图如图5-1所示。中级软件设计师,历年真题,2014年上半年(下午)《软件设计师》真题【C++代码】#include<iostream>#include<vector>using namespace std;class Observer{public:virtual void update(float temp,float humidity,float cleanness)=0;};class Subject{public:virtual void registerObserver(Observer*o)=0;//注册对主题感兴趣的观察者virtual void removeObserver(Observer*o)=0;//删除观察者virtual void notifyObservers(  )=0;//当主题发生变化时通知观察者};class EnvironmentData:public(1){private:vector<Observer*>observers;float temperature,humidity,cleanness;public:void registerObserver(Observer*o){observers.push_back(o);}void removeObserver(Observer*o){/*代码省略*/}void notifyObservers(  ){for(vector<Observer*>::const_iterator it=observers.begin(  );it!=observers.end(  );it++){(2);}}Void measurementsChanged(  ){(3);}void setMeasurements(float temperature,float humidity,float cleanness){this->temperature=temperature;this->humidity=humidity;this->cleanness=cleanness;(4);}};class CurrentConditionsDisplay:public(5){private:float temperature,humidity,cleanness;Subject*envData;public:CurrentConditionsDisplay(Subject*envData){this->envData=envData;(6);}void update(float temperature,float humidity,float cleanness){this->temperature=temperature;this->humidity=humidity;this->cleanness=cleanness;display(  );}void display(  ){/*代码省略*/}};int main(  ){EnvironmentData*envData=new EnvironmentData(  );CurrentConditionsDisplay*currentDisplay=new CurrentConditionsDisplay(envData);envData->setMeasurements(80,65,30.4f);return 0;}
答案解析:

相关问题
关于我们 | 用户指南 | 版权声明 | 给我留言 | 联系我们 | 积分商城 | 答案求助 | 网站地图
Copyright © 2024 www.daanwo.com All Rights Reserved