1

There are a lot of materials about Protocols & Delegates on the web, and different tutorials call some of the "participants" in the process by different names.

especially confusing is the "Adaptor" and the "Conformer" concepts.

I understand that the Class that is implementing the Protocol methods - i.e. the one that has the <ProtocolName> statement in it - is "Conforming to Protocol". Is it also the "Adopter" of the protocol?

How do I call the class in which the Protocol is declared at, or being re-declared at, so it can include a Property that must conform to the Protocol - i.e. have in its Interface a @protocol ProtocolName statement?

I couldn't find any Apple official documentation that explains that.

EDIT

Guys, thanks for the specified answers, but I do (and did) understand the pattern and how it's implemented in the language. My issues were (and still are) relating to the NAMING... and still, I could not find any reference to "what is the name (conceptually) of the class that DECLARES/REDECLARES the Protocol". Maybe it has no name, and I should just call it: "The Class in Which the Protocol is Declared/Redeclared". Maybe I can call it "The Delegating Class" (which sounds good, by the way)...

Ohad Regev
  • 5,641
  • 12
  • 60
  • 84
  • 3
    I have never hard them referred to as "Adopters" or "Conformers" I have heard the statement "Conforms to Protocol" before but I have always known them and they have always been referred to as Protocols in everything I have read. It is probably just be but if they are referring to them as something other then what they are they probably aren't very good tutorials and I would steer clear of them. If you write a tutorial you shouldn't use some terminology that no one knows. They are Protocols and Delegates that all they are and should be referred to as. – Popeye Nov 27 '13 at 10:09
  • @Ohad Regev. I'm not sure that it's the source of confusion, so I'm sorry if asking will offend you, by do you know that `adopt` and `adapt` are different English words? – ilya n. Nov 27 '13 at 10:22
  • Ilya, well, I did make a mistake there, and I do understand the difference between these words... but thanks for the correction... Either way, the question remains - which class is the ADOPTER... and more importantly (if it is not the adopter), how do I call the class in which the Protocol was declared/redeclared - which is the one where the methods are actually being called (while the delegate functions as the receiver of the methods). – Ohad Regev Nov 27 '13 at 11:02

5 Answers5

3

Semantics. I would say its action vs result.

By adopting a protocol (action), you're conforming to it (result)

fguchelaar
  • 4,779
  • 23
  • 36
2

Protocols

When talking about Objective-C, protocol is a concept of Objective-C language, therefore something that can be understood by a compiler. Specifically, you can define it by @protocol keyword.

A class can declare itself to conform to protocol with the angle syntax:

 @interface MyClass <MyProtocol> // class MyClass conforms to protocol MyProtocol

Some people use the word adopt here, as in "class MyClass adopts protocol MyProtocol".

This syntax will be understood by the Objective-C compiler, for example it can warn you if there are required methods in MyProtocol that are not implemented in MyClass.

Patterns

(Programming Design) Patterns are abstract concepts that can be implemented in any (Turing-complete) language. You write a language-specific code and then tell to humans about this pattern. The compiler will not know whether you call the code some fancy word or not.

Adapter is a specific pattern. It, again, can be used with any programming language, although some languages, e.g. Python make this easier with declarations.

I'm not aware of Conformer concept.

Delegates

Delegate is part of Delegation Pattern. It's used to solve a problem where an object A wants to do something to object B and then wants B to be able to talk to A. In order to make this conversation possible B will need to know something about methods of A, but if A is a very complicated class and therefore B may be forced to know "too much".

The delegation pattern solves this problem by explicitly declaring a protocol DelegateOfB, defined where B is defined. Then any class that needs to receive information from B (such as A) declares that it conforms to DelegateOfB and implements corresponding methods. Therefore B does not know anything about A other than the fact that it conforms to DelegateOfB.

This pattern can also be implemented in any language, but Cocoa/Cocoa Touch or most other Objective-C frameworks are unusual in that delegation is used 90% of time whenever the aforementioned problem arises.

Here it helps that protocols are a language feature. Again, this pattern could theoretically be used in any language, even assembler :), but it will be more useful for Objective-C, because you are able to declare that a delegate needs to conform to this protocol:

@property id<DelegateOfB> delegate;

and the compiler warns you if it thinks you assign something that doesn't conform to DelegateOfB.

In other languages, different solutions, such as callback functions, are usually used.

ilya n.
  • 18,398
  • 15
  • 71
  • 89
2

Refer to the post.

A protocol, declared with the (@protocol syntax in Objective-C) is used the declare a set of methods that a class that "adopts" (declares that it will use this protocol) will implement. This means that you can specify in your code that, "you don't care which class is used so long as it implements a particular protocol". This can be done in Objective-C as follows:

id<MyProtocol> instanceOfClassThatImplementsMyProtocol;

If you state this in your code, then any class that "conforms" to the protocol MyProtocol can be used in the variable instanceOfClassThatImplementsMyProtocol. This means that the code that uses this variable knows that it can use whichever methods are defined in MyProtocol with this particular variable, regardless of what class it is. This is a great way of avoiding the inheritance design pattern, and avoids tight coupling.

Delegates are a use of the language feature of protocols. The delegation design pattern is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol. This particular protocol specifies methods that the delegate class should implement to perform specific actions at given events. The class that uses the delegate knows that its delegate coforms to the protocol, so it knows that it can call the implemented methods at given times. This design pattern is a great way of decoupling the classes, because it makes it really easy to exchange one delegate instance for another - all the programmer has to do is ensure that the replacement instance or class conforms to the necessary protocol (i.e. it implements the methods specified in the protocol)!

Protocols and delegates are not restricted only to Objective-C and Mac/iOS development, but the Objective-C language and the Apple frameworks make heavy use of this awesome language feature and design pattern.

Here's an example. In the UIKit framework of Cocoa Touch, there is a UITextFieldDelegate protocol. This protocol defines a series of methods that classes which are delegates of a UITextField instance should implement. In other words, if you want to assign a delegate to a UITextField (using the delegate property), you'd better make sure that this class conforms to UITextFieldDelegate. In fact, because the delegate property of UITextField is defined as:

@property(nonatomic, assign) id<UITextFieldDelegate> delegate

Then the compiler will give warnings if you assign a class to it that doesn't implement the protocol. This is really useful. You have to state that a class implements a protocol, and in saying that it does, you're letting other classes know that they can interact in a particular way with your class. So, if you assign an instance of MyTextFieldDelegateClass to the delegate property of UITextField, the UITextField knows that it can call some particular methods (related to text entry, selection etc.) of your MyTextFieldDelegateClass. It knows this because MyTextFieldDelegateClass has said that it will implement the UITextFieldDelegate protocol.

Ultimately, this all leads to much greater flexibility and adaptability in your project's code, which I'm sure you'll soon realise after using this technology! Enjoy :)

Community
  • 1
  • 1
Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • 1
    If you copy an answer from another question, you might consider naming the source. Nicely copied from: http://stackoverflow.com/questions/17192287/what-exactly-are-protocols-and-delegates-and-how-are-they-used-in-ios – fguchelaar Nov 27 '13 at 10:42
0

Here Programming With Objective-C apple doc, which briefly stated about the terms you are looking for like adopt & conforms etc.

Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
  • 3
    Please try to add furthermore informations about your link, in case of dead link, your answer will be totally useless :) – Edelweiss Nov 27 '13 at 10:20
0

I'm not quite sure what you're asking. But you can force the class, that uses your protocol, to implement a certain method.

@protocol theDelegate <NSObject>

@required
-(void)doSomething;
@end
Mikael
  • 3,572
  • 1
  • 30
  • 43