第七天-delegate设计模式

//
//  main.m
//  07-delegate设计模式
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
/*
  设计模式:
         大牛们在软件件开发过程中总结出来的经验
 
  delegate:
  1、当A类对象发生什么事想通知B类型对象 (B类对象成为A类对象的代理)
  2、A类中有些事情,自己无法处理,需要B类对象帮助其处理 (B类对象成为A类对象的代理)
  3、B类对象向知道A类对象中发生的事情 (B类对象成为A类对象的代理)
 
  婴儿 与 保姆
 
   婴儿想吃的时候要通知保姆
 
 
*/
#import "CZTeacher.h"
#import "CZBaby.h"
#import "CZStudent.h"
#import "CZNurse.h"
int main(int argc, const char * argv[]) {
 
   CZBaby *baby = [[CZBaby alloc] init];
 
    CZTeacher *teacher = [[CZTeacher alloc] init];
//        baby.baomu = teacher;
    
//    CZStudent *stu = [[CZStudent alloc] init];
//    baby.baomu = stu;

    CZNurse *nurse = [[CZNurse alloc] init];
    baby.baomu = nurse;
    
    [baby wantToEat];
    
    
    return 0;
}

//
//  CZBaby.h
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>


@class CZTeacher;
@class CZStudent;
@class CZNurse;

@interface CZBaby : NSObject

//@property (nonatomic,strong) CZTeacher *baomu;
//@property (nonatomic,strong) CZStudent *baomu;

@property (nonatomic,strong) CZNurse *baomu;

//用于记录婴儿吃了多个克奶粉
@property (nonatomic,assign) int food;

- (void) wantToEat;


@end

//
//  CZBaby.m
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "CZBaby.h"
#import "CZTeacher.h"
#import "CZStudent.h"
#import "CZNurse.h"

@implementation CZBaby


- (void) wantToEat
{
    [self.baomu feedBabay:self];
}

@end

//
//  CZTeacher.h
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
@class CZBaby;

@interface CZTeacher : NSObject

//喂婴儿
- (void) feedBabay:(CZBaby *) baby;

@end

//
//  CZTeacher.m
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "CZTeacher.h"
#import "CZBaby.h"

@implementation CZTeacher

- (void) feedBabay:(CZBaby *) baby
{
    
    baby.food += 20;
    NSLog(@"%s,food = %d",__func__,baby.food);
}

@end


//
//  CZStudent.h
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
@class CZBaby;

@interface CZStudent : NSObject


//喂婴儿
- (void) feedBabay:(CZBaby *) baby;

@end

//
//  CZStudent.m
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "CZStudent.h"
#import "CZBaby.h"
@implementation CZStudent

//喂婴儿
- (void) feedBabay:(CZBaby *) baby
{
    baby.food += 30;
    NSLog(@"%s,food = %d",__func__,baby.food);
}

@end


//
//  CZNurse.h
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
@class CZBaby;

@interface CZNurse : NSObject
//喂婴儿
- (void) feedBabay:(CZBaby *) baby;
@end

//
//  CZNurse.m
//  1202-分类
//
//  Created by Apple on 14/12/2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "CZNurse.h"
#import "CZBaby.h"
@implementation CZNurse

//喂婴儿
- (void) feedBabay:(CZBaby *) baby
{
    baby.food += 15;
    NSLog(@"%s,food = %d",__func__,baby.food);
}
@end


你可能感兴趣的:(第七天-delegate设计模式)