Obj-C 实现设计模式 -- Observer
觀察者模式,采用氣象站的例子來(lái)說(shuō)明,本質(zhì)上跟Java來(lái)實(shí)現(xiàn)差不多。只不過(guò)是針對(duì)協(xié)議(Delegate)來(lái)編程。
簡(jiǎn)單說(shuō)下需求,氣象顯示版向氣象站注冊(cè)成功訂閱者(觀察者),氣象站監(jiān)測(cè)到氣溫發(fā)生變化,向各個(gè)已注冊(cè)的氣象顯示版發(fā)出通知。
遵守針對(duì)接口編程的原則,先來(lái)寫(xiě)Delegate.
第一個(gè),需要被氣象站實(shí)現(xiàn)的訂閱和取消訂閱的方法。
#import "ObserverModel.h"@protocol SubjectProtocol <NSObject>- (void) registerObserver:(id<ObserverModel>) obs; - (void) removeObserver:(id<ObserverModel>) obs; - (void) notifyObservers;@end上面被register和remove的是一個(gè)實(shí)現(xiàn)了ObserverModel的對(duì)象。
@protocol ObserverModel <NSObject> - (void) updateWithTemp:(float) temp withHumidity:(float)humidity withPressure:(float)pressure; @end@protocol DisplayDelegate <NSObject> - (void) display; @endOK,下面進(jìn)行氣象站的具體編碼。氣象站需要四個(gè)屬性,溫度、濕度、氣壓、訂閱者集合。
@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:(id<ObserverModel>)obs; - (void) removeObserver:(id<ObserverModel>)obs; - (void) notifyObservers; - (void) measurementChanged; - (void) setMeasurementsWithTemp:(float)temp withHumidity:(float)humidity withPressure:(float)pressure; @end對(duì)應(yīng)方法的實(shí)現(xiàn),
@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:(id<ObserverModel>)obs{[_objArray addObject:obs]; }- (void) removeObserver:(id<ObserverModel>)obs{int i = [_objArray indexOfObject:obs];if (i>=0) {[_objArray removeObjectAtIndex:i];} }- (void) notifyObservers{for (int i = 0; i < [_objArray count]; ++i) {id<ObserverModel> obj = (id<ObserverModel>)[_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]; }氣象顯示板的實(shí)現(xiàn),初始化的時(shí)候應(yīng)該把氣象站出入,使得該氣象顯示板決定是否向氣象顯示板注冊(cè)訂閱氣象。
@interface CurrentConditionsDisplay : NSObject <ObserverModel,DisplayDelegate>{float _temperature;float _humidity;id<SubjectProtocol> _weatherData; } - (void) CurrentConditionsDisplayWithObj:(id<SubjectProtocol> ) weatherData; - (void) updateWithTemp:(float)temp withHumidity:(float)humidity withPressure:(float)pressure; @end同時(shí),顯示板應(yīng)該實(shí)現(xiàn)接口中的屬性更新方法和顯示方法。
- (void) CurrentConditionsDisplayWithObj:(id<SubjectProtocol> ) 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); }最后,進(jìn)行整體的實(shí)例化,看看氣象站是不是能正常工作了。
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];轉(zhuǎn)載于:https://www.cnblogs.com/andywordsworth/archive/2012/04/12/2445019.html
總結(jié)
以上是生活随笔為你收集整理的Obj-C 实现设计模式 -- Observer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux防火墙开启某端口命令行,lin
- 下一篇: 本周ASP.NET英文技术文章推荐[03