Difference between revisions of "Protocol"

From GNUstepWiki
Jump to navigation Jump to search
(Formal / Informal Protocol)
(Added to category Objective-C)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Formal Protocols ==
+
A protocol is a  declaration of a group of methods not associated with any particular class.
 +
 
 +
== Formal Protocol ==
  
 
Formal Protocols are used to define the [[Interface]] for a set of methods, but not the implementation or any data members.
 
Formal Protocols are used to define the [[Interface]] for a set of methods, but not the implementation or any data members.
Line 15: Line 17:
  
  
== Informal Protocols ==
+
== Informal Protocol ==
  
 
Informal Protocols are constructs that allow you to share a common [[Interface]] between two classes without them having to inherit from any specific object (the notable exception being NSObject).
 
Informal Protocols are constructs that allow you to share a common [[Interface]] between two classes without them having to inherit from any specific object (the notable exception being NSObject).
Line 29: Line 31:
 
      
 
      
 
     @end
 
     @end
 +
 +
== Implementing Protocols ==
 +
 +
Formal and informal protocols take slightly different implementation paths. For formal protocols, you enclose the protocol name in less than/greater than signs e.g.:
 +
 +
    @interface MyClass <MyFormalProtocol>
 +
   
 +
    /* Methods in protocol */
 +
   
 +
    -(void) someMethod;
 +
    -(id) someOtherMethod;
 +
   
 +
    /* Your methods */
 +
   
 +
    @end   
 +
 +
With informal protocols, you place the protocol name in brackets after your class name, both in the interface and implemenation i.e.:
 +
 +
    @interface MyClass (MyInformalProtocol)
 +
 +
and
 +
 +
    @implementation MyClass (MyInformalProtocol)
 +
 +
Although the interface declaration is optional.
 +
 +
 +
[[Category:Objective-C]]

Latest revision as of 09:47, 21 June 2006

A protocol is a declaration of a group of methods not associated with any particular class.

Formal Protocol

Formal Protocols are used to define the Interface for a set of methods, but not the implementation or any data members.

This is useful for defining an interface which objects will need to conform to in order to take advantage of some service or other.

Example:

   @protocol MyFormalProtocol
   
   -(void) someMethod;
   -(id) someOtherMethod;
   
   @end


Informal Protocol

Informal Protocols are constructs that allow you to share a common Interface between two classes without them having to inherit from any specific object (the notable exception being NSObject).

They are generally made with a category on NSObject that has no implementation associated with it.

Example:

   @interface NSObject (MyInformalProtocol)
   
   - (id) someMethod;
   - (void) anotherMethod;
   
   @end

Implementing Protocols

Formal and informal protocols take slightly different implementation paths. For formal protocols, you enclose the protocol name in less than/greater than signs e.g.:

   @interface MyClass <MyFormalProtocol>
   
   /* Methods in protocol */
   
   -(void) someMethod;
   -(id) someOtherMethod;
   
   /* Your methods */
   
   @end    

With informal protocols, you place the protocol name in brackets after your class name, both in the interface and implemenation i.e.:

   @interface MyClass (MyInformalProtocol)

and

   @implementation MyClass (MyInformalProtocol)

Although the interface declaration is optional.