[非凡程序员]观察者模式

#import <Foundation/Foundation.h>

#import "Student.h"

#import "Goods.h"

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Student *stu=[[Student alloc]init];

        stu.name=@"Student_one";

        

        //----------- 通知 --------------

        //通知步骤: 注册/接收 --> 通知 --> 移除

        

        

         //注册(某个对象 student + 接收的方法(@select(自定义方法名))

        //注意:每当写入注册对象,请在注册对象里写上接收方法。

        //参数说明:addObserver: 观察者 selector:@select(接收方法)

        //接收方法最好加冒号,为了参数传参数。name: 通知名称 object:通知发送人 可为nil

        [[NSNotificationCenter defaultCenter] addObserver:stu selector:@selector(backChangeMessage:) name:@"notificationStudent" object:nil];

        //通知方法 一般为发送通知消息

        //注意:通知名称必须为已注册的通知名称,发送的通知可为任意对象类型

        //参数说明:postNotificationName: 通知名称 objects: 被发送消息

        [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationStudent" object:stu];

        

        //----- 拉模型 可用delegate实现 -----

        //拉模型: 由商品给出更改消息,仅仅是消息,由Person主动去拉取Goods信息

        //Goods -> setPrice 通过更改price值,回调通知消息给Person类, Person主动去拉去值

        

        Goods *good=[[Goods alloc]init];

        good.price=12;

        

        Student *xiaoMing=[[Student alloc]init];

        xiaoMing.name=@"XiaoMing";

        

        xiaoMing.goods=good;

        good.stu=xiaoMing;

        

        [[NSNotificationCenter defaultCenter] addObserver:xiaoMing selector:@selector(backMessage:) name:@"goodsNotification" object:nil];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"goodsNotification" object:good];

        good.price=22;

  }

    return 0;

}



@protocol myProtocol <NSObject>

@optional


@property(nonatomic,assign)float price;


-(void)changeMethod;

@end



#import <Foundation/Foundation.h>

#import "goodsProtocol.h"

#import "Student.h"

@interface Goods : NSObject<myProtocol>

@property(nonatomic,strong)id<myProtocol> stu;


@end




@implementation Goods

@synthesize price=_price;

-(void)setPrice:(float)price{

    _price=price;

    [_stu changeMethod];

}


@end



#import <Foundation/Foundation.h>

#import "goodsProtocol.h"

@class Goods;

@interface Student : NSObject<myProtocol>

@property(nonatomic,strong)NSString *name;

@property(nonatomic,weak)id<myProtocol> goods;


-(void)backChangeMessage:(NSNotification *)notifacation;

-(void)backMessage:(NSNotification *)goodsNotification;


@end



#import "Student.h"


@implementation Student


-(void)changeMethod{

    NSLog(@"changeMethod ...");

    NSLog(@"%f",_goods.price);

}



-(void)backChangeMessage:(NSNotification *)notifacation{

    Student *student=notifacation.object;

    NSLog(@"name:%@",student.name);

    

}



-(void)backMessage:(NSNotification *)goodsNotification{

    NSLog(@"%@",goodsNotification.object);

//    _goods=goodsNotification.object;

//    [_goods ]

}


-(void)dealloc{

    //移除 防止内存泄漏

    //方法说明: removeObserver: 观察者 name: 通知名称 object 发送人 该方法为移除观察者的消息通知

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notificationStudent" object:nil];

     //方法说明: removeObserver: 观察者  该方法为移除观察者对象

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"goodsNotification" object:nil];

    //方法说明: removeObserver: 观察者  该方法为移除观察者对象

    [[NSNotificationCenter defaultCenter] removeObserver:_goods];

    

    

    

}



@end


你可能感兴趣的:([非凡程序员]观察者模式)