代理设计模式Demo

main.m

//
//  main.m
//  代理设计模式Demo
//
//  Created by yaomars on 16/4/29.
//  Copyright © 2016年 yaomars. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Baby.h"
#import "Nurse.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        //初始化一个baby对象
        Baby *baby = [[Baby alloc]initWithName:@"Mark" andSleepValue:60 andHungerValue:70.0f];
        //创建一个nurse对象
        Nurse *nurse = [[Nurse alloc]init];
        
        //指定baby的代理对象为nurse对象
        baby.delegate = nurse;
        
        
        //baby调用需要吃东西的方法
        [baby cryWithFood:@"进口牛奶"];
        
        NSLog(@"=========================================");
        
        //baby调用需要睡觉的方法
        [baby crywithSleep];

        
    }
    return 0;
}

TakeCareOfBabyProtocol.h

//
//  TakeCareOfBabyProtocol.h
//  HM笔试总结
//
//  Created by yaomars on 16/4/29.
//  Copyright © 2016年 yaomars. All rights reserved.
//

#import <Foundation/Foundation.h>

//照顾baby的协议
@protocol TakeCareOfBabyProtocol <NSObject>

//协议中声明的方法
-(void)TakeCareOfBabyWithFood:(NSString *)foodName andHungerValue:(float)hungerValue;
-(void)TakeCareOfBabyWithSleep:(int)sleepValue;

@end

Nurse.h

//
//  Nurse.h
//  HM笔试总结
//
//  Created by yaomars on 16/4/29.
//  Copyright © 2016年 yaomars. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TakeCareOfBabyProtocol.h"

@interface Nurse : NSObject<TakeCareOfBabyProtocol>  //保姆遵守了照顾baby的协议

@end

Nurse.m

//
//  Nurse.m
//  HM笔试总结
//
//  Created by yaomars on 16/4/29.
//  Copyright © 2016年 yaomars. All rights reserved.
//

#import "Nurse.h"

@implementation Nurse

//实现照顾baby协议中的方法

-(void)TakeCareOfBabyWithFood:(NSString *)foodName andHungerValue:(float)hungerValue{
    
    NSLog(@"Nurse正在给baby喂适合的%@,并且哄哄baby!",foodName);
    NSLog(@"饥饿值变化为:%.2f",hungerValue + 5.0f);
    
}

-(void)TakeCareOfBabyWithSleep:(int)sleepValue{

    NSLog(@"Nurse正在哄着baby,慢慢让baby睡觉...");
    NSLog(@"睡眠值变化为:%i",sleepValue + 10);
}

@end


Baby.h

//
//  Baby.h
//  HM笔试总结
//
//  Created by yaomars on 16/4/29.
//  Copyright © 2016年 yaomars. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TakeCareOfBabyProtocol.h"

@interface Baby : NSObject

@property (nonatomic,copy) NSString *name;      //baby的名字
@property (nonatomic,assign) int sleepValue;     //睡眠值
@property (nonatomic,assign) float hungerValue;  //饥饿值

//拥有一个遵守照顾baby协议的对象(baby吃饭和睡觉的行为需要由代理来完成)
@property (nonatomic,assign) id<TakeCareOfBabyProtocol> delegate;

//自定义构造方法
-(instancetype)initWithName:(NSString *)name andSleepValue:(int)sleepValue andHungerValue:(float)hungerValue;

//baby需要吃东西的方法
-(void)cryWithFood:(NSString *)foodName;
//baby需要睡觉的方法
-(void)crywithSleep;

@end

Baby.m

//
//  Baby.m
//  HM笔试总结
//
//  Created by yaomars on 16/4/29.
//  Copyright © 2016年 yaomars. All rights reserved.
//

#import "Baby.h"

@implementation Baby

-(instancetype)initWithName:(NSString *)name andSleepValue:(int)sleepValue andHungerValue:(float)hungerValue{

    if (self = [super init]) {
        _name = name;
        _sleepValue = sleepValue;
        _hungerValue = hungerValue;
    }
    return self;
}

-(void)cryWithFood:(NSString *)foodName{

    NSLog(@"baby哭了,需要吃东西了.");
    [self.delegate TakeCareOfBabyWithFood:foodName andHungerValue:self.hungerValue];
    
}

-(void)crywithSleep{
    NSLog(@"baby哭了,需要睡觉觉了...");
    [self.delegate TakeCareOfBabyWithSleep:self.sleepValue];
}

@end




版权声明:本文为u012851982原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。