Table of Contents
Protocol-Oriented Programming
Return to Swift Protocols - Swift Protocol-Oriented Programming - Swift POP, Swift, Protocol-Oriented Programming - Protocol (object-oriented programming)
See:
- Snippet from Wikipedia: Interface (object-oriented programming)
In object-oriented programming, an interface or protocol type is a data type that acts as an abstraction of a class. It describes a set of method signatures, the implementations of which may be provided by multiple classes that are otherwise not necessarily related to each other. A class which provides the methods listed in a protocol is said to adopt the protocol, or to implement the interface.
If objects are fully encapsulated then the protocol is the only way in which they may be accessed by other objects. For example, in Java, the
Comparable
interface specifies a methodcompareTo()
which implementing classes must implement. This means that a sorting method, for example, can sort a collection of any objects of types which implement 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 ofcompareTo()
).Some programming languages provide explicit language support for protocols (Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, OCaml, PHP, Racket, Seed7, Swift, Python 3.8). In languages supporting multiple inheritance, such as C++, interfaces are implemented as abstract classes.
In languages without explicit support, protocols are often still present as conventions. This is known as duck typing. For example, in Python, any class can implement an
__iter__
method and be used as a collection.Type classes in languages like Haskell, or module signatures in ML and OCaml, are used for many of the things that protocols are used for.
In Rust, interfaces are called traits.
Fair Use Sources
OOP: Category OOP, Object (computer science), Object-orientation, Object-oriented programming (OOP), Comparison of programming languages (object-oriented programming), Comparison of programming paradigms, Component-based software engineering, Design by contract, Object association, Object database, Object model reference, Object modeling language, Object-oriented analysis and design, Object-relational impedance mismatch (and The Third Manifesto), Object-relational mapping, Protocol-Oriented Programming - Protocol (object-oriented programming) - Swift Protocol-Oriented Programming. (navbar_oop)
© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers
SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.