【说明】
某实验室欲建立一个实验室环境监测系统,能够显示实验室的温度、湿度以及洁净度等环境数据。
当获取到最新的环境测量数据时,显示的环境数据能够更新。
现在采用观察者(Observer)模式来开发该系统。
观察者模式的类图如图6-1所示。
java
import java.util.*;
interface Observer {
pubilc void update(float temp, float humidity, float cleanness);
}
interface Subject {
pubilc void registerObserver(Observer o)
pubilc void removeObserver(Observer o)
pubilc void notifyObservers()
}
class EnvironmentData implements Subject{
private ArrayList observers;
private float temperature,humidity,cleanness;
pubilc EnvironmentData(){
observers = new ArrayList()
}
pubilc void registerObserver(Observer o){
observers.add(o)
}
pubilc void removeObserver(Observer o){}
pubilc void notifyObservers(){
for(int i=0;i<observers.size();i++){
Observer observers = (Observer)observers.get(i);
Observer.update(temperature,humidity,cleanness)
}
}
pubilc void measurementsChanged(){
notifyObservers();
}
pubilc void setMeasurements(float temperature,float humidity, float cleanness){
this.temperature = temperature
this.humidity = humidity
this.cleanness = cleanness
measurementsChanged();
}
}
class CurrentConditionsDisplay implements Observer(){
private float temperature;
private float humidity;
private float cleanness;
private Subject envData;
pubilc CurrentConditionsDisplay(Subject envData){
this.envData = envData
envData.registerObserver(this)
}
pubilc void update(float temperature,float humidity,float cleanness){
this.temperature = temperature
this.humidity = humidity
this.cleanness = cleanness
display();
}
pubilc void display(){}
}
class EnvironmentMonitor{
pubilc static void main(String[] args){
EnvironmentData envData = new EnvironmentData()
CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(envData)
envData.setMeasurements(80,64,30.4f)
}
}
C++
using namespace std
class Observer {
pubilc virtual void update (float temp,float cleanness) = 0
}
class Subject {
pubilc virtual void registerObserver(Observer*o) = 0
virtual void removeObserver(Observer*o) = 0
virtual void notifyObserver() = 0
}
class EnvironmentData:pubilc Subject{
private: vector<Observer*> observer;
float temperature,humidity,cleanness;
pubilc: void registerObserver(Observer*o){
observer.push_back(o)
}
void removeObserver(Observer*o){}
void notifyObserver(){
for(vector<Observer*>::const_iterator it = observer.begin();it!=observer.end();it++){
(*it)->update(temperature,humidity,cleanness)
}
}
void measurementsChanged(){
notifyObserver();
}
void setMeasurements(float temperature,float humidity, float cleanness){
this->temperature = temperature
this->humidity = humidity
this->cleanness = cleanness
measurementsChanged();
}
}
class CurrentConditionsDisplay:pubilc Observer(){
private:float temperature,humidity,cleanness;
Subject*envData;
pubilc:CurrentConditionsDisplay(Subject*envData){
this->envData = envData
this->envData->registerObserver(this)
}
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,64,30.4f)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)