iOS-合理運用設(shè)計模式-責(zé)任鏈模式-信息校驗

需求:用戶的信息錄入校驗

當(dāng)前界面的布局我是基于UIScrollview布局的,每一個單元格是UIView而不是Cell。


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

看普通的實現(xiàn)方法

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

假如使用了責(zé)任鏈模式

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

責(zé)任鏈模式 一篇很詳細(xì)的介紹。
責(zé)任鏈模式主要降低了請求發(fā)送者和接收者之間的耦合度笙什。使得多個對象都有機會處理某一個請求。在請假系統(tǒng)中可能需要多個人物對請假進行審批。采購系統(tǒng)中可能需要不同高度的人進行審批婉烟。一個請求可能由多個對象處理,但是不清楚具體接收者對象暇屋。所以將對象連成一條鏈子似袁,使請求沿著這條鏈子傳遞下去。直到被處理為止、這就是責(zé)任鏈模式昙衅。

我們?nèi)绾卧诖a中實現(xiàn)扬霜?

首先定義一個責(zé)任鏈消息

#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

然后創(chuàng)建一個管理類,管理你所有的責(zé)任鏈

#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

到這個步驟而涉,其實我們就已經(jīng)封裝成功了著瓶。
那我們?nèi)绾问褂貌拍軣o侵入性,耦合性最少呢啼县?直接上代碼

#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的對象添加責(zé)任鏈屬性材原。

針對業(yè)務(wù)我們要如何去使用呢?

  • 封裝業(yè)務(wù)
    首先具體業(yè)務(wù)具體分析季眷,使用繼承基鏈的方式余蟹,來定制我們的業(yè)務(wù)。目前我們的需求是: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
  • 業(yè)務(wù)模塊的使用
    首先導(dǎo)入管理類和業(yè)務(wù)鏈類
//責(zé)任鏈
#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基于不同的業(yè)務(wù)去判斷我們想要的類型挺峡。

最后我們回到校驗的地方

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

工具責(zé)任鏈github地址
備注:同事黃哥設(shè)計整理葵孤,我認(rèn)為這是很好的編程思想,所以總結(jié)加搬運和分享橱赠。
如有侵權(quán)尤仍,請告知謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末病线,一起剝皮案震驚了整個濱河市吓著,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌送挑,老刑警劉巖绑莺,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異惕耕,居然都是意外死亡纺裁,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門司澎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來欺缘,“玉大人,你說我怎么就攤上這事挤安⊙枋猓” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵蛤铜,是天一觀的道長嫩絮。 經(jīng)常有香客問我丛肢,道長,這世上最難降的妖魔是什么剿干? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任蜂怎,我火速辦了婚禮,結(jié)果婚禮上置尔,老公的妹妹穿的比我還像新娘杠步。我一直安慰自己,他們只是感情好榜轿,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布幽歼。 她就那樣靜靜地躺著,像睡著了一般差导。 火紅的嫁衣襯著肌膚如雪试躏。 梳的紋絲不亂的頭發(fā)上猪勇,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天设褐,我揣著相機與錄音,去河邊找鬼泣刹。 笑死助析,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的椅您。 我是一名探鬼主播外冀,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼掀泳!你這毒婦竟也來了雪隧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤员舵,失蹤者是張志新(化名)和其女友劉穎脑沿,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體马僻,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡庄拇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了韭邓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片措近。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖女淑,靈堂內(nèi)的尸體忽然破棺而出瞭郑,到底是詐尸還是另有隱情,我是刑警寧澤鸭你,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布屈张,位于F島的核電站我抠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏袜茧。R本人自食惡果不足惜菜拓,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望笛厦。 院中可真熱鬧纳鼎,春花似錦、人聲如沸裳凸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽姨谷。三九已至逗宁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間梦湘,已是汗流浹背瞎颗。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留捌议,地道東北人哼拔。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像瓣颅,于是被迫代替她去往敵國和親倦逐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353