iPhone Development

Just another iPhone Blog

@protocol – nothing but a set of rules

Abstract classes in C++, Interfaces in Java, @protocols in Objective C.. They all serve the same purpose

What are protocols??

As described in the title, they define Set of Rules

When do we use them??

“When two strangers (In our OOPs world, they refer to two objects whose identity is not known to each other) need to establish a communication, we use protocols to bridge the communication gap between them”

Let us see the usage of protocols with a real world example

Let us say two members from a different smuggling gang say GangA and GangB needs to exchange the commodities between them. Then in order to prove their identities they follow certain rules, in other words Protocols,described below

Gang A to Gang B : tell me the secret code Gang A to Gang B : secret code accepted Gang B to Gang A : share your secret code Gang B to Gang A : your secret code is accepted

If the conversation happens to be in order above, then we can say the Gang A and Gang B members are valid persons

So, Let us transform this whole example into our Programming world using Objective C language by Defining two classes for Gang A and Gang B

GangA.h

@interface GangA:NSObject<GangAProtocol>

{

NSObject<GangBProtocol> * gangBMember;

}

@property(nonatomic,assign)NSObject<GangBProcotol>* gangBMember

@end

/* Defines what conversation is expected in order to speak with GangB member.. So The Gang B members should implement these methods to prove  */

@protocol GangBProtocol

-(NSString*)tellMeTheSecretCode;//Request the GangB member for secret code

-(void)secretCodeAccepted:(BOOL)isAccepted;//Inform whether they are Authenticated or not

@end

 

GangB.h

@interface GangB:NSObject<GangBProtocol> {

NSObject<GangAProtocol> gangAMember;

} @property(nonatomic,assign)NSObject<GangAProtocol> gangAMember; @end

/* Defines what conversation is expected in order to speak with GangA member.. So The Gang A members should implement these methods to prove their identity  */

@protocol GangBProtocol

-(NSString*)shareYourSecretCode;//Request the GangB member for secret code

-(void)yourCodeIsAccepted:(BOOL)isAccepted;//Inform whether they are Authenticated or not

@end

Most of the code is self-explatory. To summarize

* GangA class would have access to a GangB member which implements GangBProtocol which helps GangA member to establish communication and check their identity and the same holds for GangB class

* Each class would declare a Protocol on how the other gang members would be expected to respond to their queries and implement the methods in the protocol defined by other gang class i.e. GangA class declares GangB protocol on how it expects to communicate with GangB while implementing the GangA protocol defined in GangB class

[Although the protocols in the two classes in the example means the same, i have just added them to illustrate the situation where two objects of unknown identity communicates by aggreeing on to some common terms] To Summarize, Protocols are used

* When we want to establish communication between two object whose identity or Class type is not required to be known to each other

* When we want to establish communication between two objects, where one object knows the class type to which it is communicating while other Objects need not know the identity of the object at the other end. Eg: Delegate mechanism

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: