Python: Building data abstractions
Theory: Interfaces
The term interface is widespread in IT and everyday life. For example, a user interface can be a set of controls for a Web site, an ATM, or a telephone. The interface of a TV remote control is the buttons. The interface in a car would be the levers, buttons, and steering wheel. In summary, the interface determines how you interact with a given system.
Designing interfaces isn't as easy as it might seem. We encounter inconvenient interfaces — when we open doors or use elevators. The more complex the system, the more possible states, the more difficult it is to make the interface. Even in a primitive example of a TV power button with two states — on/off.
You can implement one or two buttons that behave differently depending on the current state.
Programming is similar. An interface is a set of functions with names and signatures that specify the number and types of incoming parameters and the return value, which doesn't depend on any particular implementation. This definition corresponds to the concept of abstract data types. For example, for points, all the functions we have implemented in practice and described in theory are interface functions.
How are the concepts of abstraction and interface related? Abstraction is a word that primarily describes the data we work with. For example, almost every web application contains a user abstraction. Hexlet has the abstractions course, project, and others. An interface is a set of functions helping to interact with data.
But there aren't just interface functions, but also helper functions that are not designed for calling code and are used exclusively within the abstraction:
When we work with complex abstractions represented by external libraries, the number of non-interface functions is significantly greater than the number of interface ones. For example, we may have one or two in the interface but hundreds of them in the library.
The quality of your abstraction can be judged, among other things, by how convenient its interface is.