Reputation: 7623
what are the Advantages of using protocols in Objective-C, when we require them? I have read the objective-C programming guide it shows mainly 3 advantages:
To declare methods that others are expected to implement.
To declare the interface to an object while concealing its class
To capture similarities among classes that are not hierarchically related.
Can any one explain me (2) and (3), I am not able to get it, as I have read details of them in Obj-C programming Guide? Are there any other advantages of using protocol? How can you describe why we need protocols in Objective-C?
Upvotes: 0
Views: 1794
Reputation: 1403
2) To declare the interface to an object while concealing its class
@protocol DatabaseConnection
- (QueryResult*) runQuery:(NSString*)query;
- (void) close;
@end
@interface MySQLConnection : NSObject <DatabaseConnection> {
...
}
@interface OracleConnection : NSObject <DatabaseConnection> {
...
}
// imagine a method on some class that will give you a connection
- (DatabaseConnection*) getConnection {
// some logic to determine which connection to return
if (something)
return [[[OracleConnection alloc] init] autorelease];
else
return [[[MySQLConnection alloc] init] autorelease];
}
and you using it like so in client code ...
id <DatabaseConnection> con = [SomeClass getConnection];
QueryResult* result = [con runQuery:@"select * from users"];
[con close];
Notice how we are abstracted away from the actual database implementation and can run our query regardless of which database we're talking with.
The advantage of defining DatabaseConnection
as a protocol as opposed to a base class is that we don't have to provide a default implementation. Because in this case, the default implementation would not make sense. A protocol is Objective-C's way of doing a purely abstract base class in C++ or an interface in Java.
3) To capture similarities among classes that are not hierarchically related.
You might want the ability to ask any object about which class it implements.
- (Class)class;
You might also want to know what its superclass is.
- (Class)superclass;
Perhaps you want to ask if any two objects are equal.
- (BOOL)isEqual:(id)object;
All of these things and more are methods you may want on classes such as File, MySQLConnection, User, MonsterX, MiniGun. Though these classes are entirely different from an inheritance point of view, which is why they can't share a base class, a protocol addresses this need.
For example, Objective-C has a base protocol called NSObject.
Note: There will be some who point out that most Objective-C classes inherit from the NSObject class and that they get a number of these methods from there.
This statement is true. But NSObject
the class, implements NSObject
the protocol. Apple recognized that there will be cases when your objects do not inherit from NSObject
the class, but may still want to provide services such as responding to the description
method.
Upvotes: 3
Reputation: 19315
It helps you implement polymorphism
. Basically once you have a protocol set up, any object implementing that protocol can answer to a particular method.
It's almost like an API for an object: an expected behavior.
If you have an emailMe
protocol, any object that supports it would be guaranteed to take a sendToAddress
. It doesn't matter what kind of object it is, just that it can handle a emailMe
message.
Then it's up to the object itself to package the relevant content from itself to get emailed out. But this way you can have one sender
class that can choose to send a variety of objects via email. It doesn't matter that all of those objects are different classes, just that they support the protocol emailMe
.
Upvotes: 0
Reputation: 1316
3) You can implement similar functionality (by responding to the same set of messages) (as in 1) without directly subclassing another class which is the same.
For instance, you have Class Something1 which implements some functionality that you want in Something2. You can't subclass Something1 because you are already a subclass of a view controller, say. By using protocols you can link them without them being hierarchically related (one is a subclass of the other).
I do not quite understand 2 myself, so if anyone else can shed light on that...
Upvotes: 0