05 – Protocol for Interface

Java Interface

Interfaces are used very havily inside of java applications. They allow the programmer to decouple different parts of the code and to make them independent.

Additionally they are used to define a contract which specifies methods which have to be implemented by subclasses. It is very comfortable to code against interfaces, rather than implementations.

Interface

Interface implementation

The following syntax is used to implement an interface into a class:

class MyClass implements SomeInterface

 

Objective-C Protocols

Objective-C provides a similar approach called Protocols. Protocols have a very close semantic as interfaces, but allow you to mark methods as @optional. Optional methods can be implemented by subclasses, but it is not required.

Protocols are defined by the @protocol directive.

@protocol SomeProtocol
    .... methods
@end

The SoundMachine protocol would look like this:

It’s really looks very close to the java declaration. Also the implementation class of this interface is very similar to java.

The .h file

and the .m file

 

ConformsToProtocol

If you would like to test whether a class implements a protocol you can use the conformsToProtocol message. It is similar to the instanceof check from java.

Objective-C: conformsToProtocol

Java: instanceof

 

@optional methods

In contrast to Java, Objective-C adds an additional semantic feature for protocols. Methods can be defined as optional. This means, that they do not have to be implemented by the class that implements the interface.

Java does not allow unimplemented interface methods. All the methods from an interface have to be declared in the classes type hierarchy.

The following image shows how optional methods can be implemented in Objetive-C:

If a class implements this protocol but does not add a method implementation for the optional methods, the compiler won’t mention this issue.

Therefore i have prepared an MyProtocolImpl.m class which implements MyProtocol. No methods have been implemented in the class before compiling it.

As you can see at the screenshot, the compiler only mentioned the missing required methods.

Calling an optional unimplemented method

If you are providing optional protocol methods, you have to ensure that messages are sent to these classes only if they implement the methods addressed by the message. Otherwise the call will result in an error.

The following image shows an implementation of MyProtocol. The anotherOptional-method is NOT implemented. A call to it would result in an error.

Therefore i have prepared a second class which will call the missing method by sending a message.

During the call of the anotherOptional-method call the Objective-c runtime sends the following error to the message console.

2011-03-10 12:56:02.923 MS4[21571:a0f] -[MyProtocolImpl anotherOptional]:
                        unrecognized selector sent to instance 0x1001611a0

To avoid such errors, the call schould be protected by the respondsToSelector message. This message will return FALSE, if the receiver of the message does not implement a method which responds to the message.

Since the receiver does not implement the method, it will not be called.

 

Additional informations

developer.apple.com

Advertisements

Tags: , , , , , , , ,

One Response to “05 – Protocol for Interface”

  1. definition of semiotics by saussure Says:

    Howdy! I just want to give a huge thumbs up for the nice info you have got right here
    on this post. I will be coming back to your weblog for
    extra soon.

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: