Protocol (object-oriented programming)
In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other. These are definitions of methods and values which the objects agree upon in order to co-operate.[1]
For example, in Java (where protocols are termed interfaces), the Comparable interface specifies a method compareTo() which implementing classes should implement. This means that a separate sorting method, for example, can sort any object which implements the Comparable interface, without having to know anything about the inner nature of the class (except that two of these objects can be compared by means of compareTo()).
The protocol is a description of:
- The messages that are understood by the object.
- The arguments that these messages may be supplied with.
- The types of results that these messages return.
- The invariants that are preserved despite modifications to the state of an object.
- The exceptional situations that will be required to be handled by clients to the object.
If the objects are fully encapsulated then the protocol will describe the only way in which objects may be accessed by other objects.
Some programming languages provide explicit language support for protocols or interfaces (Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, PHP, Racket, Seed7, Swift). In C++ interfaces are known as abstract base classes and implemented using pure virtual functions. The object-oriented features in Perl also support interfaces.
Although the Go programming language is not generally considered an object-oriented language, it does allow methods to be defined on user-defined types. Go has "interface" types that are compatible with any type that supports a given set of methods (the type does not need to explicitly implement the interface). The empty interface, interface{}
, is compatible with all types.
Note that functional programming and distributed programming languages have a concept which is also called a protocol, but whose meaning is subtly different (i.e. a specification of allowed exchanges of messages, emphasis on exchanges, not on messages). This difference is due to somewhat different assumptions of functional programming and object-oriented programming paradigms. In particular, the following are also considered as part of a protocol in these languages:
- The allowed sequences of messages,
- Restrictions placed on either participant in the communication,
- Expected effects that will occur as the message is handled.
Type classes in languages like Haskell are used for many of the things that protocols are used for.
See also
- Concept (generic programming)
- Delegation (programming)
- Protocols in Objective-C
- Class (computer science)
- Encapsulation (computer science)
- Public interface
- List of basic computer science topics
- Application programming interface
References
- ↑ "The Objective-C programming Language:Protocols". Apple Inc. Retrieved 7 October 2012.