Responsibility Chain of Design Patterns

Posted by Mardoxx on Wed, 15 May 2019 10:13:09 +0200

Chain of Responsibility Model

Concept and purpose

Also known as the responsibility chain model

Avoid coupling the sender of a request with the receiver so that multiple objects have the opportunity to process the request. Connect the receiving object into a chain and pass the request along the chain until one object can process it.

Responsibility chain model can decouple sender and receiver. Sender only needs to send out messages. Receiver is responsible for processing messages. Sometimes in our work, we may encounter such a situation. When you are on business or you work overtime, you need to send email to the leader, and the leader needs to send email to his leader, so that the report up to the next level. You can get your hard work only after the leader who can give you reimbursement approval. If you have had a fight with one of the leaders before, when it's over, people will not report it to you, then you will be miserable.

UML

Example

Man wears armor and magic resistance. If he attacks a sword, the armor can defend him; if he attacks magic damage, the magic resistance can defend him; otherwise, he will be injured.

1. Defender superclass

#import <Foundation/Foundation.h>
#import "GongJi.h"

@interface Handler : NSObject
// Next responder
@property (nonatomic, strong) Handler *nextHandler;

// Interface for processing requests
- (void)handleRequest:(GongJi *)request;
@end
#import "Handler.h"

@implementation Handler

- (void)handleRequest:(GongJi *)attack {
    // If you can't respond, just forward the request to successor for processing.
    [self.nextHandler handleRequest:attack];
}
@end

1.1 Imitation Defense Subclass

#import "Handler.h"

@interface MoKangKuiJia : Handler

@end
#import "MoKangKuiJia.h"
#import "MoFa.h"

@implementation MoKangKuiJia
-(void)handleRequest:(GongJi *)request {

    if ([request isKindOfClass:[MoFa class]]) {
        NSLog(@"2.The attack did not pass through this magic armor.");

    } else {
        NSLog(@"2.It's not a magic attack.,Cannot resist,Others to deal with---%@", [MoFa class]);
        [self.nextHandler handleRequest:request];
    }
}
@end

1.2 House Defense Subclass

@interface KuiJia : Handler

@end
#import "KuiJia.h"
#import "WuQi.h"

@implementation KuiJia

-(void)handleRequest:(GongJi *)request {

    if ([request isKindOfClass:[WuQi class]]) {
        NSLog(@"1.The attack did not pass through the armor.");

    } else {
        NSLog(@"1.Not a sword attack,Cannot resist,Others to deal with---%@", [WuQi class]);
        [self.nextHandler handleRequest:request];
    }
}
@end

1.3 as the last defender

#import "Handler.h"

@interface Person : Handler

@end
#import "Person.h"

@implementation Person
- (void)handleRequest:(GongJi *)request {
    NSLog(@"cover--%@--It hurts.",[request class]);
}
@end

2 attacker

@interface GongJi : NSObject

@end
@implementation GongJi

@end

2.1 indoor injuries

@interface WuQi : GongJi

@end
@implementation WuQi

@end

2.2 Magic Damage

@interface MoFa : GongJi

@end
@implementation MoFa

@end

3 call

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 1. Creating Characters
    Handler *person = [[Person alloc] init];

    // 2. Increase Magic Shield
    Handler *moKangKuiJia = [[MoKangKuiJia alloc] init];
    moKangKuiJia.nextHandler = person;

    // 3. wear armor.
    Handler *kuiJia = [[KuiJia alloc] init];
    kuiJia.nextHandler = moKangKuiJia;


    // 1. Weapon Attack
    GongJi *wuqi = [[WuQi alloc] init];
    [kuiJia handleRequest:wuqi];

    NSLog(@"----------------");
    // 2. Magic Attack
    GongJi *mofa = [[MoFa alloc] init];
    [kuiJia handleRequest:mofa];

      NSLog(@"----------------");
    // 3. Lightning attack
    GongJi *sd = [[ShanDian alloc] init];
    [kuiJia handleRequest:sd];
}

Effect

supplement


This is the UIView response function for event handling in iOS, which we can see as an example of the responsibility chain pattern.

Topics: iOS