场景:我们要从openfire服务器获取消息,然后同步更新到最近联系人界面和正在聊天的界面。此系统必须可扩展(公布一组API,让其他开发人员也可以写出自己的即时通讯),也就是用户可以自己增加或者删除接收消息的界面。
直接上代码:
父类
@interface Subject : NSObject
//注册监听者
- (void)registObserver:(Observer *)o;
//移除监听者
- (void)removeObserver:(Observer *)o;
//通知监听者
- (void)notifyObserver;
@end
@interface Observer : NSObject
//更新
- (void)updateDispayWithContent:(NSString *)content fromPeople:(NSString *)from toPeople:(NSString *)to;
@end
子类:
@interface Openfire : Subject
//发送消息
- (void) sendMessage:(NSString *)content fromPoeple:(NSString *)from toPeople:(NSString *)to;
@end
@interface Openfire()
@property (retain, nonatomic) NSMutableArray *observers;
@property (retain, nonatomic) NSString *fromPeople;
@property (retain, nonatomic) NSString *toPeople;
@property (retain, nonatomic) NSString *messageContent;
@end
@implementation Openfire
- (void)dealloc{
[self.observers release];
self.observers = nil;
[super dealloc];
}
- (id)init{
self = [super init];
if (self) {
self.observers = [[NSMutableArray alloc] init];
}
return self;
}
//创建数据库单例模式
+ (id)shareOpenfire{
static dispatch_once_t openfireToken;
static id shareOpenfire = nil;
dispatch_once(&openfireToken, ^{
shareOpenfire = [[[self class] alloc] init];
});
return shareOpenfire;
}
//注册监听者
- (void)registObserver:(Observer *)o{
[self.observers addObject:o];
}
//移除监听者
- (void)removeObserver:(Observer *)o{
NSInteger i = [self.observers indexOfObject:o];
if (i>=0 && i<[self.observers count]) {
[self.observers removeObjectAtIndex:i];
}
}
//通知监听者
- (void)notifyObserver{
for (int i=0; i<[self.observers count]; i++) {
Observer *o = [self.observers objectAtIndex:i];
[o updateDispayWithContent:self.messageContent fromPeople:self.fromPeople toPeople:self.toPeople];
}
}
//收到消息
- (void)receiveMessage{
[self notifyObserver];
}
//发送消息
- (void) sendMessage:(NSString *)content fromPoeple:(NSString *)from toPeople:(NSString *)to{
self.messageContent = content;
self.fromPeople = from;
self.toPeople = to;
[self receiveMessage];
}
@end
@interface Chat : Observer
//初始化
- (id)initWithSubject:(Subject *)obj;
@end
@interface Chat()
@property (retain, nonatomic) Subject *openfire;
@end
@implementation Chat
- (void)dealloc{
if (self.openfire) {
[self.openfire removeObserver:self];
[self.openfire release];
self.openfire = nil;
}
[super dealloc];
}
//初始化
- (id)initWithSubject:(Subject *)obj{
self = [super init];
if (self) {
if (obj) {
self.openfire = obj;
[self.openfire registObserver:self];
}
}
return self;
}
//更新
- (void)updateDispayWithContent:(NSString *)content fromPeople:(NSString *)from toPeople:(NSString *)to{
[self displayMessage:content fromPeople:from toPeople:to];
}
//显示
- (void)displayMessage:(NSString *)content fromPeople:(NSString *)from toPeople:(NSString *)to{
NSLog(@"chat收到消息:%@ , from:%@ , to:%@", content, from, to);
}
@end
测试代码;
Openfire *o = [[Openfire alloc] init];
Chat *c = [[Chat alloc]initWithSubject:o];
Recent *r = [[Recent alloc]initWithSubject:o];
[o sendMessage:@"测试。。。" fromPoeple:@"lichenglong" toPeople:@"ping"];
[o sendMessage:@"test。。。" fromPoeple:@"lichenglong" toPeople:@"li"];
[o sendMessage:@"hello。。。" fromPoeple:@"lichenglong" toPeople:@"lee"];
[o sendMessage:@"你好。。。" fromPoeple:@"lichenglong" toPeople:@"chen"];
[o release];
[c release];
[r release];