1. Agent First Knowledge
Suppose the boss needs to buy a computer for the worker to buy
The boss needs an agent to buy a computer for the worker.
Boss creates a protocol protocol for the method of buying a computer
Bosses have a class of attribute properties that conform to the protocol
Boss has a member method method implementation for the agent to buy a computer
Worker classes need to follow protocol implementation protocol method buy a computer
The code is implemented as follows
1 #import <Foundation/Foundation.h>
2 #import "Boss.h"
3 #import "Worker.h"
4
5 int main(int argc, const char * argv[]) {
6 @autoreleasepool {
7
8 Boss * boss = [[Boss alloc] init];
9 Worker * worker = [[Worker alloc] init];
10 [boss setDelegate:worker];
11 [boss delegateBuyMacbook];
12
13 }
14 return 0;
15 }
main.m
1 #import <Foundation / Foundation.h>
2
3 // Create agreement
4 @protocol BossDelegate <NSObject>
5
6-(void) buyMacbook;
7
8 @end
9
10
11 @interface Boss: NSObject
12 // There is an agent who abides by the agreement
13 @property (nonatomic, weak) id <BossDelegate> delegate;
14 // Method calls agent to buy computer
15-(void) delegateBuyMacbook;
16 @end
Boss.h
1 #import "Boss.h"
2
3 @implementation Boss
4-(void) delegateBuyMacbook {
5 NSLog (@ "The boss started looking for someone to buy a computer");
6 [_delegate buyMacbook];
7}
8 @end
BOSS.M
1 #import <Foundation/Foundation.h>
2 #import "Boss.h"
3 @interface Worker : NSObject<BossDelegate>
4
5 @end
Worker.h
1 #import "Worker.h"
2
3 @implementation Worker
4-(void) buyMacbook {
5 NSLog (@ "Bought 60 Macbook Pro 15 high with");
6}
7 @end
WORKER.M
Objective-c Agent