ObjectiveC(9)_代理(委托)设计模式

版权声明:本博客主要记录学习笔记和遇到的一些问题解决方案,转载请注明出处! https://blog.csdn.net/u010982507/article/details/82891416

代理(委托)设计模式

代理是指一个对象提供机会对另一个对象中的行为发生变化时作出的反应。代理设计模式的基本思想是两个对象协同解决问题,通常用于对象之间的通信。基本特点有:

  • 简化了对象的行为,最小化了对象之间的耦合度
  • 使用代理,一般来说无需子类化
  • 简化了应用程序的开发,既容易实现,又灵活

NSRunloop概念

NSRunloop就是一个事件处理的循环,用来不停的调度工作以及处理输入事件。使用runloop的目的是让你的线程在有工作的时候忙于工作,而没工作的时候处于休眠状态。在应用程序中,不需要创建NSRunloop对象。因为在主线程(包含其他子线程)系统中会自动创建NSRunloop对象。如果你需要访问当前线程中的runloop,可以通过类方法currentRunloop调用。

中介找房代理设计模式的实现

一、需求分析

  • 租客需要找房,委托中介人去找房
  • 中介人负责找房
  • 协议合同规定了租客与中介人的法律效益

二、代码实现
1、 创建租客Person

  • 创建Person.h类
#import <Foundation/Foundation.h>
#import "ApartmentProtocol.h" // 使用当前协议
@protocol PersinDelegate; // 协议的声明,不声明的话就要把协议写在类前面 
@interface Person : NSObject
{
@private
    NSString *name;
    id <ApartmentProtocol> _delegate;
    id <PersinDelegate> _delegates;
}
@property (nonatomic,copy) NSString *name;
@property (nonatomic) id <ApartmentProtocol> _delegate; //assign防止循环引用
- (id)initWithName:(NSString *)name withDelegate: (id <ApartmentProtocol>) delegate;
- (void)personFindApartment;
@end
  • 创建Person.m实现类
#import "Person.h"
// 定义私有方法
@interface Person()
- (void) startFindApartment:(NSTimer *)timer;
@end
@implementation Person
@synthesize name,_delegate;
// 初始化租客姓名和中介
- (id) initWithName:(NSString *)name withDelegate: (id) delegate
{
    if (self == [super init]) {
        self.name = name;
        self._delegate = delegate;
    }
    return self; 
}
// 租客找房方法
- (void) personFindApartment
{
	// 使用定时器调用startFindApartment找房方法,startFindApartment内部方法,在实现类内部使用
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@"Tom" repeats:YES];
}

// 开始找房
- (void) startFindApartment:(NSTimer *)timer
{
    NSString *userInfo = [timer userInfo];
    NSLog(@"%@",userInfo);
    if ([self._delegate respondsToSelector:@selector(findApartment:)]) {
       HouseRent rent = [self._delegate findApartment:self];
        if (rent == HighRent) {
            NSLog(@"%@说太贵",self.name);
        } else if (rent == MiddleRent){
            NSLog(@"%@说价格合适",self.name);
        } else if (rent == LowRent){
            NSLog(@"%@说价格不错,就要这套了",self.name);
            [timer invalidate]; // 停掉定时器
        } else if (rent == Finding){
            NSLog(@"%@说找到通知我",self.name);
        }
    }
}
@end

2、创建中介人Agent

  • 创建Agent.h
#import <Foundation/Foundation.h>
#import "ApartmentProtocol.h"
@interface Agent : NSObject <ApartmentProtocol> //实现ApartmentProtocol协议

@end
  • 创建Agent.m
#import "Agent.h"
#import "Person.h"
@implementation Agent

static int count = 3;
- (HouseRent)findApartment:(Person *) p;
{
    HouseRent rent;
   if(count == 1){
        rent = LowRent;
        NSLog(@"中介公司对%@说,找到一个价格较低的公寓",p.name);
       
    } else if(count == 2){
        rent = MiddleRent;
        NSLog(@"中介公司对%@说,找到一个价格合适的公寓",p.name);
    } else if(count == 3){
        rent = HighRent;
        NSLog(@"中介公司对%@说,找到一个价格较高的公寓",p.name);
    } else {
        NSLog(@"中介公司对%@说,正在找房",p.name);
        rent = Finding;
    }
    count --;
    return rent;
}
@end

3、 创建协议ApartmentProtocol

#import <Foundation/Foundation.h>
// 创建租金枚举
typedef enum {
    Finding = 0,
    LowRent = 1,
    MiddleRent = 2,
    HighRent = 3
    
} HouseRent;
@class Person; // 引入Person类,并不使用
@protocol ApartmentProtocol <NSObject>
@required
- (HouseRent)findApartment:(Person *) p;
@end

4、方法调用

#import <Foundation/Foundation.h>
#import "Agent.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
    	// 初始化中介
        Agent *agent = [[Agent alloc] init];
        // 初始化租客
        Person *tom = [[Person alloc] initWithName:@"tom" withDelegate: agent];
        // 调用租客找房方法
        [tom personFindApartment];
        BOOL isStop = YES;
        while (isStop) {
            NSDate *date = [NSDate date];
            // 6秒之后退出程序
            [[NSRunLoop currentRunLoop] runUntilDate:[date dateByAddingTimeInterval:6]];
            isStop = NO;
            NSLog(@"程序退出了 ");
        }
        // [[NSRunLoop currentRunLoop] run];
        
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u010982507/article/details/82891416
今日推荐