日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

Obj-C 实现设计模式 -- Observer

發(fā)布時(shí)間:2023/12/9 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Obj-C 实现设计模式 -- Observer 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

觀察者模式,采用氣象站的例子來(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; @end

OK,下面進(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。