码迷,mamicode.com
首页 > 移动开发 > 详细

iOS.常用设计模式.02.委托模式

时间:2014-06-08 20:57:13      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

WTDelegate
bubuko.com,布布扣
#import <Foundation/Foundation.h>

@protocol WTDelegate <NSObject>

@required

- (void)sleep;

- (void)eat;

- (void)work;

@end
bubuko.com,布布扣
WTPhilosopher.h
bubuko.com,布布扣
#import <Foundation/Foundation.h>
#import "WTDelegate.h"

@interface WTPhilosopher : NSObject
{
    NSTimer *timer;
    int count;
}
@property (nonatomic,weak) id<WTDelegate> delegate;

- (void)start;

- (void)handle;

@end
bubuko.com,布布扣
WTPhilosopher.m
bubuko.com,布布扣
#import "WTPhilosopher.h"

@implementation WTPhilosopher
@synthesize delegate;

- (void)start
{
    count = 0;
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(handle) userInfo:nil repeats:YES];
}
- (void)handle
{
    switch (count) {
        case 0:
            [self.delegate sleep];
            count++;
            break;
        case 1:
            [self.delegate eat];
            count++;
            break;
        case 2:
            [self.delegate work];
            [timer invalidate];
            break;
            
        default:
            break;
    }
}

@end
bubuko.com,布布扣
WTViewController.m
bubuko.com,布布扣
#import "WTViewController.h"
#import "WTPhilosopher.h"

@interface WTViewController ()

@end

@implementation WTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    WTPhilosopher *philosopher = [[WTPhilosopher alloc] init];
    philosopher.delegate = self;
    [philosopher start];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -- WTDelegate 方法实现
-(void) sleep
{
    NSLog(@"sleep...");
}

-(void) eat
{
    NSLog(@"eat...");
}

-(void) work
{
    NSLog(@"work...");
}

@end
bubuko.com,布布扣

 

iOS.常用设计模式.02.委托模式,布布扣,bubuko.com

iOS.常用设计模式.02.委托模式

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/cqchen/p/3775844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!