How to resolve the algorithm Delegates step by step in the Objective-C programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Delegates step by step in the Objective-C programming language
Table of Contents
Problem Statement
A delegate is a helper object used by another object. The delegator may send the delegate certain messages, and provide a default implementation when there is no delegate or the delegate does not respond to a message. This pattern is heavily used in Cocoa framework on Mac OS X. See also wp:Delegation pattern. Objects responsibilities: Delegator: Delegate: Show how objects are created and used. First, without a delegate, then with a delegate that does not implement "thing", and last with a delegate that implements "thing".
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Delegates step by step in the Objective-C programming language
Source code in the objective-c programming language
#import
@interface Delegator : NSObject {
id delegate;
}
- (id)delegate;
- (void)setDelegate:(id)obj;
- (NSString *)operation;
@end
@implementation Delegator
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)obj {
delegate = obj; // Weak reference
}
- (NSString *)operation {
if ([delegate respondsToSelector:@selector(thing)])
return [delegate thing];
return @"default implementation";
}
@end
// Any object may implement these
@interface NSObject (DelegatorDelegating)
- (NSString *)thing;
@end
@interface Delegate : NSObject
// Don't need to declare -thing because any NSObject has this method
@end
@implementation Delegate
- (NSString *)thing {
return @"delegate implementation";
}
@end
// Example usage
// Memory management ignored for simplification
int main() {
// Without a delegate:
Delegator *a = [[Delegator alloc] init];
NSLog(@"%d\n", [[a operation] isEqualToString:@"default implementation"]);
// With a delegate that does not implement thing:
[a setDelegate:@"A delegate may be any object"];
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
// With a delegate that implements "thing":
Delegate *d = [[Delegate alloc] init];
[a setDelegate:d];
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
return 0;
}
#import
// Formal protocol for the delegate
@protocol DelegatorDelegatingProtocol
- (NSString *)thing;
@end
@interface Delegator : NSObject
@property (weak) id delegate;
- (NSString *)operation;
@end
@implementation Delegator
- (NSString *)operation {
if ([self.delegate respondsToSelector: @selector(thing)])
return [self.delegate thing];
return @"default implementation";
}
@end
@interface Delegate : NSObject
@end
@implementation Delegate
- (NSString *)thing { return @"delegate implementation"; }
@end
// Example usage with Automatic Reference Counting
int main() {
@autoreleasepool {
// Without a delegate:
Delegator *a = [Delegator new];
NSLog(@"%@", [a operation]); // prints "default implementation"
// With a delegate that does not implement thing:
a.delegate = @"A delegate may be any object";
NSLog(@"%@", [a operation]); // prints "default implementation"
// With a delegate that implements "thing":
Delegate *d = [Delegate new];
a.delegate = d;
NSLog(@"%@", [a operation]); // prints "delegate implementation"
}
return 0;
}
You may also check:How to resolve the algorithm Host introspection step by step in the Wren programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the R programming language
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the Java programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Groovy programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Jsish programming language