bakgrund
Protocol (object-oriented programming)
Wikipedia (en)
Protocol is a term used by particular object-oriented programming languages with a variety of specific meanings, which other languages may term interface or trait.
When used otherwise, "protocol" is akin to a communication protocol, indicating the chain of interactions between the caller and the object.
Languages which use the term "protocol" include:
Clojure
Elixir
Java 8
Logtalk
Objective-C
Swift
PythonIn these languages, a protocol is a common means for discrete objects to communicate with each other. These are definitions of methods and values which the objects agree upon, in order to co-operate, as part of an API.
The protocol/interface 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.
(For the communications-style usage only:) The call sequence and decision points of the methods, such as would be represented in UML interaction diagrams: Communication diagram, Sequence diagram, Interaction overview diagram/Activity diagram, Timing diagram.If the objects are fully encapsulated then the protocol will describe the only way in which objects may be accessed by other objects. For example, in Java interfaces, the Comparable interface specifies a method compareTo() which implementing classes must 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()).
Some programming languages provide explicit language support for protocols/interfaces (Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, OCaml, PHP, Racket, Seed7, Swift). In C++ interfaces are known as abstract base classes, and are implemented using pure virtual functions. In OCaml, object-oriented interfaces are known as class types. The object-oriented features in Perl also support interfaces.