[非凡程序員]觀察者模式

#import <Foundation/Foundation.h>post

#import "Student.h"atom

#import "Goods.h"spa

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

    @autoreleasepool  {orm

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

        stu.name=@"Student_one";對象

        

        //----------- 通知 --------------內存

        //通知步驟: 註冊/接收 --> 通知 --> 移除rem

        

        

         //註冊(某個對象 student + 接收的方法(@select(自定義方法名))get

        //注意:每當寫入註冊對象,請在註冊對象裏寫上接收方法。

        //參數說明:addObserver: 觀察者 selector:@select(接收方法)

        //接收方法最好加冒號,爲了參數傳參數。name: 通知名稱 object:通知發送人 可爲nil

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

        //通知方法 通常爲發送通知消息

        //注意:通知名稱必須爲已註冊的通知名稱,發送的通知可爲任意對象類型

        //參數說明:postNotificationName: 通知名稱 objects: 被髮送消息

        [[NSNotificationCenter defaultCenterpostNotificationName:@"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 defaultCenteraddObserver:xiaoMing selector:@selector(backMessage:) name:@"goodsNotification" object:nil];

        [[NSNotificationCenter defaultCenterpostNotificationName:@"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 defaultCenterremoveObserver:self name:@"notificationStudent" object:nil];

     //方法說明: removeObserver: 觀察者  該方法爲移除觀察者對象

    [[NSNotificationCenter defaultCenterremoveObserver:self];

    

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

    //方法說明: removeObserver: 觀察者  該方法爲移除觀察者對象

    [[NSNotificationCenter defaultCenterremoveObserver:_goods];

    

    

    

}



@end

相關文章
相關標籤/搜索