iOS-合理运用设计模式-责任链模式-信息校验

需求:用户的信息录入校验

当前界面的布局我是基于UIScrollview布局的,每一个单元格是UIView而不是Cell。


212BF4F7-E8E0-400F-A844-E987949AFE6B.png

看普通的实现方法

5BB1F858-244C-4C23-AD3A-4448B5588801.png

假如使用了责任链模式

81E749FC-BAD4-4D9A-8594-1A1B52096486.png

责任链模式 一篇很详细的介绍。
责任链模式主要降低了请求发送者和接收者之间的耦合度。使得多个对象都有机会处理某一个请求。在请假系统中可能需要多个人物对请假进行审批。采购系统中可能需要不同高度的人进行审批。一个请求可能由多个对象处理,但是不清楚具体接收者对象。所以将对象连成一条链子,使请求沿着这条链子传递下去。直到被处理为止、这就是责任链模式。

我们如何在代码中实现?

首先定义一个责任链消息

#import <Foundation/Foundation.h>
@interface ResponsibilityMessage : NSObject
@property (nonatomic, weak)   id object;//校验对象
@property (nonatomic) BOOL       checkSuccess;//校验成功
@property (nonatomic, strong) id errorMessage;//校验失败
@end

#import "ResponsibilityMessage.h"
@implementation ResponsibilityMessage
@end

其次定义一个基类链

#import <Foundation/Foundation.h>
#import "ResponsibilityMessage.h"
@interface ResponsibilityChain : NSObject
/**
 The object to attach.附加对象
 */
@property (nonatomic, weak) id object;
/**
 Overwrite by subclass.
 @return Can pass through or not.是否可以通过
 */
- (ResponsibilityMessage *)canPassThrough;
@end

#import "ResponsibilityChain.h"
@implementation ResponsibilityChain
- (ResponsibilityMessage *)canPassThrough {
    ResponsibilityMessage *message = [ResponsibilityMessage new];
    message.checkSuccess           = YES;
    message.object                 = self.object;
    return message;
}
@end

然后创建一个管理类,管理你所有的责任链

#import <Foundation/Foundation.h>
#import "NSObject+ResponsibilityChain.h"
@interface ResponsibilityManager : NSObject
/**
 Get all the chains.
 */
@property (nonatomic, strong, readonly) NSArray *chains;
/**
 Add chain to responsibilityManager, if the object doesn't have the value in 'responsibilityChain', it will crash.
 @param object The object that has value in 'responsibilityChain'.
 */
- (void)addChain:(NSObject *)object;
/**
 Remove chain from responsibilityManager, if the object doesn't have the value in 'responsibilityChain', it will crash.
 @param object The object that has value in 'responsibilityChain'.
 */
- (void)removeChain:(NSObject *)object;
/**
 Check all the responsibilityChains.
 @return The check message.
 */
- (ResponsibilityMessage *)checkResponsibilityChain;
#import "ResponsibilityManager.h"
@interface ResponsibilityManager ()
@property (nonatomic, strong) NSMutableArray *storeChains;
@end

@implementation ResponsibilityManager
- (instancetype)init {
    if (self = [super init]) {
        self.storeChains = [NSMutableArray array];
    }
    return self;
}

- (NSArray *)chains {
    return [NSArray arrayWithArray:self.storeChains];
}

- (void)addChain:(NSObject *)object {
    NSParameterAssert(object.responsibilityChain);
    [self.storeChains addObject:object];
}

- (void)removeChain:(NSObject *)object {
    NSParameterAssert(object.responsibilityChain);
    [self.storeChains removeObject:object];
}

- (ResponsibilityMessage *)checkResponsibilityChain {
    ResponsibilityMessage *message = nil;
    for (NSObject *chain in self.storeChains) {
        message = [chain.responsibilityChain canPassThrough];
        if (message.checkSuccess == NO) {
            break;
        }
    }
    return message;
}
@end

到这个步骤,其实我们就已经封装成功了。
那我们如何使用才能无侵入性,耦合性最少呢?直接上代码

#import <Foundation/Foundation.h>
#import "ResponsibilityChain.h"
@interface NSObject (ResponsibilityChain)
@property (nonatomic, strong) ResponsibilityChain *responsibilityChain;
@end
#import "NSObject+ResponsibilityChain.h"
#import <objc/runtime.h>
@implementation NSObject (ResponsibilityChain)
- (void)setResponsibilityChain:(ResponsibilityChain *)responsibilityChain {
    responsibilityChain.object = self;
    objc_setAssociatedObject(self, @selector(responsibilityChain), responsibilityChain, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (ResponsibilityChain *)responsibilityChain {
    return objc_getAssociatedObject(self, _cmd);
}
@end

我们使用类别(分类)去给所有继承自NSObject的对象添加责任链属性。

针对业务我们要如何去使用呢?

  • 封装业务
    首先具体业务具体分析,使用继承基链的方式,来定制我们的业务。目前我们的需求是:UITextField中文本的输入校验。
#import "ResponsibilityChain.h"
@interface DefiningTermChain : ResponsibilityChain
+ (instancetype)DefiningTermChainWithErrorMessage:(NSString *)string;
@end
#import "DefiningTermChain.h"
#import "DJQSelectItemView.h"
#import "DJQTextFiledTitleView.h"
@interface DefiningTermChain ()
@property (nonatomic, strong) NSString *errorMessage;
@end
@implementation DefiningTermChain
- (ResponsibilityMessage *)canPassThrough {
    if ([self.object isKindOfClass:DJQSelectItemView.class]) {
        DJQSelectItemView           *selectView   = self.object;
        ResponsibilityMessage *message            = [ResponsibilityMessage new];
        message.object                            = self.object;
        message.checkSuccess                      = YES;
        if (selectView.content.length <= 0) {
            message.errorMessage = [NSString stringWithFormat:@"%@输入不能为空", self.errorMessage];
            message.checkSuccess = NO;
        }
        return message;
    }else if ([self.object isKindOfClass:DJQTextFiledTitleView.class]){
        DJQTextFiledTitleView *titleView  = self.object;
        ResponsibilityMessage *message   = [ResponsibilityMessage new];
        message.object                   = self.object;
        message.checkSuccess             = YES;
        if ([titleView.modelDicKey isEqualToString:@"qq"]) {
            if (titleView.field.text.length < 5) {
                message.errorMessage = @"QQ号最低五位";
                message.checkSuccess = NO;
            }
        }
        if (titleView.field.text.length <= 0) {
            message.errorMessage = [NSString stringWithFormat:@"%@输入不能为空", self.errorMessage];
            message.checkSuccess = NO;
        }
        return message;
    }else {
        UITextField           *field   = self.object;
        ResponsibilityMessage *message  = [ResponsibilityMessage new];
        message.object                  = self.object;
        message.checkSuccess           = YES;
        if (field.text.length <= 0) {
            message.errorMessage = [NSString stringWithFormat:@"%@输入不能为空", self.errorMessage];
            message.checkSuccess = NO;
        }
        return message;
    }
}
+ (instancetype)DefiningTermChainWithErrorMessage:(NSString *)string {
    DefiningTermChain *chain = [DefiningTermChain new];
    chain.errorMessage       = string;
    return chain;
}
@end
  • 业务模块的使用
    首先导入管理类和业务链类
//责任链
#import "ResponsibilityManager.h"
#import "DefiningTermChain.h"
@property (nonatomic, strong) ResponsibilityManager *responsibilityManager;
self.responsibilityManager = [ResponsibilityManager new];
//DJQTextFiledTitleView 这个类里面是包含所要显示的信息,例如校验不通过时显示的字段。
DJQTextFiledTitleView *fieldView    = [[DJQTextFiledTitleView alloc] initWithFrame:CGRectMake(0, y, Width, 50.f)];
fieldView.field.responsibilityChain = [DefiningTermChain DefiningTermChainWithErrorMessage:title];
        [self.responsibilityManager addChain:fieldView.field];

fieldView.textView.responsibilityChain = [DefiningTermChain DefiningTermChainWithErrorMessage:title];//title为校验不通过的时候显示的信息
        [self.responsibilityManager addChain:fieldView.textView];

分别是判断是否是DJQSelectItemView,DJQTextFiledTitleView, UITextField基于不同的业务去判断我们想要的类型。

最后我们回到校验的地方

if (self.responsibilityManager.chains.count > 0 && [self.responsibilityManager checkResponsibilityChain].checkSuccess == NO) {
        [self djq_showToastByMessage:[self.responsibilityManager checkResponsibilityChain].errorMessage];
        return;
}

工具责任链github地址
备注:同事黄哥设计整理,我认为这是很好的编程思想,所以总结加搬运和分享。
如有侵权,请告知谢谢。