如何建设数据报表网站,区块链开发,北京短视频拍摄公司,佛山网站建设公司怎么样观察者模式#xff0c;采用气象站的例子来说明#xff0c;本质上跟Java来实现差不多。只不过是针对协议#xff08;Delegate#xff09;来编程。 简单说下需求#xff0c;气象显示版向气象站注册成功订阅者#xff08;观察者#xff09;#xff0c;气象站监测到气温发生…观察者模式采用气象站的例子来说明本质上跟Java来实现差不多。只不过是针对协议Delegate来编程。 简单说下需求气象显示版向气象站注册成功订阅者观察者气象站监测到气温发生变化向各个已注册的气象显示版发出通知。 遵守针对接口编程的原则先来写Delegate. 第一个需要被气象站实现的订阅和取消订阅的方法。 #import ObserverModel.hprotocol SubjectProtocol NSObject- (void) registerObserver:(idObserverModel) obs;
- (void) removeObserver:(idObserverModel) obs;
- (void) notifyObservers;end 上面被register和remove的是一个实现了ObserverModel的对象。 protocol ObserverModel NSObject
- (void) updateWithTemp:(float) temp withHumidity:(float)humidity withPressure:(float)pressure;
endprotocol DisplayDelegate NSObject
- (void) display;
end OK下面进行气象站的具体编码。气象站需要四个属性温度、湿度、气压、订阅者集合。 interface WeatherData : NSObject SubjectProtocol{NSMutableArray * _objArray;float _tempurature;float _humidity;float _pressure;
}
property (nonatomic, retain) NSMutableArray * objArray;
property (nonatomic, assign) float tempurature;
property (nonatomic, assign) float humidity;
property (nonatomic, assign) float pressure;- (id) initWithArray;
- (void) registerObserver:(idObserverModel)obs;
- (void) removeObserver:(idObserverModel)obs;
- (void) notifyObservers;
- (void) measurementChanged;
- (void) setMeasurementsWithTemp:(float)temp withHumidity:(float)humidity withPressure:(float)pressure;
end 对应方法的实现 synthesize objArray _objArray;
synthesize humidity _humidity;
synthesize tempurature _tempurature;
synthesize pressure _pressure;- (id) initWithArray{self [super init];if (self) {_objArray [[NSMutableArray alloc] initWithCapacity:10];}return self;
}- (void) registerObserver:(idObserverModel)obs{[_objArray addObject:obs];
}- (void) removeObserver:(idObserverModel)obs{int i [_objArray indexOfObject:obs];if (i0) {[_objArray removeObjectAtIndex:i];}
}- (void) notifyObservers{for (int i 0; i [_objArray count]; i) {idObserverModel obj (idObserverModel)[_objArray objectAtIndex:i];[obj updateWithTemp:_tempurature withHumidity:_humidity withPressure:_pressure];}
}- (void) measurementChanged{[self notifyObservers];
}- (void) setMeasurementsWithTemp:(float)temp withHumidity:(float)humidity withPressure:(float)pressure{_tempurature temp;_humidity humidity;_pressure pressure;[self measurementChanged];
} 气象显示板的实现初始化的时候应该把气象站出入使得该气象显示板决定是否向气象显示板注册订阅气象。 interface CurrentConditionsDisplay : NSObject ObserverModel,DisplayDelegate{float _temperature;float _humidity;idSubjectProtocol _weatherData;
}
- (void) CurrentConditionsDisplayWithObj:(idSubjectProtocol ) weatherData;
- (void) updateWithTemp:(float)temp withHumidity:(float)humidity withPressure:(float)pressure;
end 同时显示板应该实现接口中的属性更新方法和显示方法。 - (void) CurrentConditionsDisplayWithObj:(idSubjectProtocol ) weatherData{_weatherData weatherData;[weatherData registerObserver:self];
}
- (void) updateWithTemp:(float)temp withHumidity:(float)humidity withPressure:(float)pressure{_temperature temp;_humidity humidity;[self display];
}- (void) display{NSLog(Temperature is %f and Humidity is %f,_temperature,_humidity);
} 最后进行整体的实例化看看气象站是不是能正常工作了。 WeatherData * weatherData [[WeatherData alloc] initWithArray];
CurrentConditionsDisplay * cCD [[CurrentConditionsDisplay alloc] init];
[cCD CurrentConditionsDisplayWithObj:weatherData];
[weatherData setMeasurementsWithTemp:20.0 withHumidity:22.0 withPressure:24.0];
[weatherData setMeasurementsWithTemp:30.0 withHumidity:32.0 withPressure:34.0]; 转载于:https://www.cnblogs.com/andywordsworth/archive/2012/04/12/2445019.html