2、责任链模式

一、描述

1、对象引用了同一个类型的另一个对象,形成一个链条。
2、链中的每一个对象实现了同样的方法,处理链中的第一个对象发起的请求。
3、如果一个对象不知道如何处理请求,就把请求传递給下一个响应器(下一个对象)。

二、适用场合

1、有多个对象可以处理请求,而处理程序只有在运行时才能确定
2、向对象发出请求,而不想显式的指定哪一个对象处理或者不指定多少个对象来处理请求。

三、优缺点

1、请求的发送者与接收者进行了分离,降低了耦合度
2、需要多个对象处理

四、实际应用-判断输入字符是不是邮箱、手机号、用户名

1、抽象处理请求(创建抽象类,或者制定协议)


#import <Foundation/Foundation.h>

@protocol ResponsibilityProtocol <NSObject>

//设置继任者
-(void)setSuccessor:(id<ResponsibilityProtocol>) successor;

//获取继任者
-(id<ResponsibilityProtocol>)successor;


//处理请求
-(void)handleRequest:(id)request;


@end


2、创建处理请求对象(QYEmail)

.h文件
#import <Foundation/Foundation.h>
#import "ResponsibilityProtocol.h"

@interface QYEmail : NSObject<ResponsibilityProtocol>

@end


.m文件
#import "QYEmail.h"
#import "RegExCategories.h"

@interface QYEmail ()

@property(nonatomic,strong)id<ResponsibilityProtocol> nextSuccessor;

@end


@implementation QYEmail

//设置继任者
-(void)setSuccessor:(id<ResponsibilityProtocol>) successor
{
    self.nextSuccessor = successor;
}

//获取继任者
-(id<ResponsibilityProtocol>)successor
{
    return self.nextSuccessor;
}


//处理请求
-(void)handleRequest:(id)request
{
    NSString *string = request;
    
    BOOL isEmail = [string isMatch:RX(@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}")];
    
    if (isEmail) {
        
        NSLog(@"%@是邮箱",string);
        
    }else{
        
        [self.successor handleRequest:string];
    
    }
}

@end



3、创建处理请求对象(QYPhoneNum)

.h文件
#import <Foundation/Foundation.h>
#import "ResponsibilityProtocol.h"


@interface QYPhoneNum : NSObject<ResponsibilityProtocol>

@end

.m文件
#import "QYPhoneNum.h"
#import "RegExCategories.h"

@interface QYPhoneNum ()

@property(nonatomic,strong)id<ResponsibilityProtocol> nextSuccessor;

@end

@implementation QYPhoneNum

//设置继任者
-(void)setSuccessor:(id<ResponsibilityProtocol>) successor
{
    self.nextSuccessor = successor;
}

//获取继任者
-(id<ResponsibilityProtocol>)successor
{
    return self.nextSuccessor;
}

//处理请求
-(void)handleRequest:(id)request
{
    NSString *string = request;
    
    BOOL isEmail = [string isMatch:RX(@"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$")];
    
    if (isEmail) {
        
        NSLog(@"%@是手机号码",string);
        
    }else{
        
        [self.successor handleRequest:string];
        
    }

}

@end


4、创建处理请求对象(QYUserName)

.h文件
#import <Foundation/Foundation.h>
#import "ResponsibilityProtocol.h"


@interface QYUserName : NSObject<ResponsibilityProtocol>

@end

.m文件
#import "QYUserName.h"
#import "RegExCategories.h"

@interface QYUserName ()

@property(nonatomic,strong)id<ResponsibilityProtocol> nextSuccessor;

@end

@implementation QYUserName


//设置继任者
-(void)setSuccessor:(id<ResponsibilityProtocol>) successor
{
    self.nextSuccessor = successor;
}

//获取继任者
-(id<ResponsibilityProtocol>)successor
{
    return self.nextSuccessor;
}

//处理请求
-(void)handleRequest:(id)request
{
    NSString *string = request;
    
    BOOL isEmail = [string isMatch:RX(@"^[A_Za-z0-9]{6,20}+$")];
    
    if (isEmail) {
        
        NSLog(@"%@是用户名",string);
        
    }else{
        
        [self.successor handleRequest:string];
        
    }
}

@end

5、创建处理请求对象(QYEndChain)

.h文件
#import <Foundation/Foundation.h>
#import "ResponsibilityProtocol.h"

@interface QYEndChain : NSObject<ResponsibilityProtocol>

@end


.m文件
#import "QYEndChain.h"

@implementation QYEndChain


//设置继任者
-(void)setSuccessor:(id<ResponsibilityProtocol>) successor
{
    
}

//获取继任者
-(id<ResponsibilityProtocol>)successor
{
    return nil;
}

//处理请求
-(void)handleRequest:(id)request
{
    NSString *string = request;
    
    NSLog(@"%@是不常用的类型",string);
        
}
@end


6、处理客户端请求

    //创建责任对象
    self.email      = [QYEmail new];
    self.PhoneNum   = [QYPhoneNum new];
    self.userName   = [QYUserName new];
    self.endChain   = [QYEndChain new];
    
    //链接责任对象
    self.email.successor    = self.PhoneNum;
    self.PhoneNum.successor = self.userName;
    self.userName.successor = self.endChain;
    
    //处理请求
    [self.email handleRequest:@"18678263339@qq.com"];


五、推荐第三方-RegExCategories

这是一个简化了Objective-C和Swift的正则表达式。

 

 

posted @ 2016-03-24 16:07  ios20  阅读(204)  评论(0编辑  收藏  举报