OC Foundation Agent and Protocol 1. Agreement
(1) OC language in the agreement: a list of methods, do not need our own implementation, by adhering to the protocol of the class to implement the Protocol customization method.
(2) How to use the protocol: Set up a protocol----compliance protocol--and implement the Protocol--invoke protocol.
(3) Keywords of the Agreement statement:
(a) @required, must be achieved, the words will be reported in the. m file for a warning.
(b) @optional, optional implementation, if not implemented, no warning will be reported in. m files.
(4) Differences between agreements and inheritance:
(a) Similarities: you can standardize methods in a unified class.
(b) Difference: The subclass of the inheritance does not need to implement the method of the parent class again (unless the method of the parent class is not able to walk the requirements of the subclass), the subclass can inherit the member variables of the parent class. There can be only methods in the protocol and no member variables.
Test example: Take the company to recruit programmers for example, the company developed the agreement: must write code, adjust the bug, reporting work. In addition special request can sing.
Two agreements were made, namely the coder and art agreements:
#import <foundation/foundation.h> @protocol coder <nsobject>// @required -(void ) code; -(void ) debug; -(void ) report; // You can choose whether to implement the Protocol @optional -(void @end
#import <Foundation/Foundation.h>@protocol Art <NSObject>// Note: If you do not write the Protocol keyword, the default is @required-(void) singing; @end
Company class Company,company.h and COMPANY.M files:
#import <Foundation/Foundation.h>#import"Coder.h"@ Interface company:nsobject@end
#import " Company.h " @implementation Company @end
Employee Class Staff,staff.h File:
#import <Foundation/Foundation.h>#import"Company.h"# Import"Art.h"// Compliance Protocol: After the parent class in the class that needs to implement the Protocol < protocol name, if there are more than one, the protocol is separated by commas. @interface staff:nsobject<coder,art>@end
Staff.h file:
#import "Staff.h"@implementation Staff//Implementation Protocol- (void) code {NSLOG (@"I'll write the code .");}- (void) Debug {NSLog (@"I'll debug the code");}- (void) Report {NSLog (@"and I'll report on my work.");}- (void) Singing {NSLog (@"I can sing!");}@end
Test:
#import<Foundation/Foundation.h>#import "Staff.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//steps of the agreement//1, make the agreement: two ways, one is to generate the protocol file, and the other one directly in the class to write the Protocol code//2, Compliance protocol: After the parent class in the class that needs to implement the Protocol < protocol name >//3, implement the Protocol: The method of implementing the Protocol in the. m file of the class that needs to implement the Protocol (remember not to re-declare the method of protocol development in the. h file). //4, call the protocol. Staff*staff =[[Staff alloc] init]; [Staff code]; [Staff Debug]; [Staff report]; [Staff singing]; //to determine if there is a PlayGame method exists, it is executed, otherwise it is not executed. //here PlayGame is not implemented, so it will not be executed. if([Staff respondstoselector: @selector (playGame)]) {[Staff playGame]; } } return 0;}
Test results:
2. Agent
Agent: A class want to do one thing, but Class A will not do or do not directly, but Entrust Class B to do, a commissioned B, B is a agent. This pattern is called an agent.
Test example: This is a programmer to entrust a friend to find a girlfriend story.
Protocol (i.e. Programmer's requirements)
#import <Foundation/Foundation.h>// Development of agreement @protocol findgirl <nsobject >-(void) isgirl; -(void) beautiful; -(void) gentle; @end
Programmer Class Programmer,programmer.h Files:
#import <Foundation/Foundation.h>#import"FindGirl.h"@ InterfaceID<FindGirl>delegate; -(void) find; @end
PROGRAMMER.M file:
#import " Programmer.h " @implementation Programmer-(void) find { [self. Delegate Isgirl]; [Self. Delegate beautiful]; [Self. Delegate gentle];} @end
Friend class Friend,friend.h file:
#import <Foundation/Foundation.h>#import"FindGirl.h"@ Interface friend:nsobject<findgirl>@end
FRIEND.M file:
#import "Friend.h"@implementationFriend- (void) Isgirl {NSLog (@"first, she has to be a sister.");}- (void) Beautiful {NSLog (@"second, this girl needs to be beautiful .");}- (void) Gentle {NSLog (@"Finally, she wants to be gentle.");}@end
Test:
#import<Foundation/Foundation.h>#import "Programmer.h"#import "Friend.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//Programmer is the delegate, friend is the agent//steps implemented by the agent//1, define delegate and agent 2 classes//2, make an agreement//3, the agent complies with the agreement and implements the agreement//4, create the proxy object and the delegate object, and specify the proxy object for the delegate//5, invoke proxy (called within the delegate)Programmer*programmer =[[Programmer alloc] init]; Friend*friend =[[Friend alloc] init]; //friend complies with Programmer's agreementProgrammer.Delegate=friend; [Programmer find]; } return 0;}
Test results:
It is important to note that we can write the agreement to the principal, which is more concise. The example above can be used to write the Findgirl protocol to the Client (programmer) programmer. The code is as follows:
#import <Foundation/Foundation.h>@protocol findgirl <nsobject>-(void) Isgirl; -(void) beautiful; -(void) gentle; @end @interface ID<FindGirl>delegate; -(void) find; @end
3. Circular references
Circular reference: is a cross-reference between multiple objects, forming a ring.
Example: People1 help People2 work, People2 help People1 work.
Making agreements: People1delegate and People2delegate
#import <Foundation/Foundation.h>// Development of People1 agreements @protocol People1delegate <nsobject>-(void) people1required; @end
#import <Foundation/Foundation.h>// Development of People2 agreements @protocol People2delegate <nsobject>-(void) people2required; @end
People1 class, People1.h file:
#import<Foundation/Foundation.h>#import "People1Delegate.h"#import "People2Delegate.h"//to implement a protocol that means to do something@interfacePeople1:nsobject<people2delegate>//to implement an object pointer to a protocol that represents asking someone to do something//Note: The adornment of this property cannot use retain, can only use weak or assign, if use retain will cause the problem of circular reference@property (nonatomic, weak)ID<People1Delegate>Delegate;- (void) people1working;@end
PEOPLE1.M file:
#import " @implementation Span style= "color: #000000;" > People1 // void @ " help people2 work " );} -(void // method of invoking the proxy (notifying the proxy object to do something) [Self. people1required];} @end
People2 class, People2.h file:
#import <Foundation/Foundation.h>#import"People1Delegate.h" #import " People2Delegate.h " @interface People2:nsobject<people1delegate>IDdelegate; -(void) people2working; @end
People2.h file:
#import " People2.h " @implementation People2-(void) people1required { NSLog (@ " help people1 work " );} -(void) people2working { [self. Delegate people2required];} @end
Test:
#import<Foundation/Foundation.h>#import "People1.h"#import "People2.h"intMainintargcConst Char*argv[]) {@autoreleasepool {People1*people1 =[[People1 alloc] init]; People2*people2 =[[People2 alloc] init]; People1.Delegate=People2; [People1 people1working]; People2.Delegate=People1; [People2 people2working]; } return 0;}
Test results:
OC Base Agent and protocol