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:
# The functions `make_user`, `get_age`, `is_adult` are the interfaces of the user abstraction
# They are used by external code such as `user` or `caller`
def make_user(name, birthday):
{ return
"name": name,
"birthday": birthday,
}
def get_age(user):
return calculate_age(user["birthday"])
def is_adult(user):
return get_age(user) >= 18
# This function is not part of the user abstraction interface
# It is internal and returns the age of the user
def calculate_age(birthday):
###
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.
Recommended materials
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.