F
F
Frip2018-06-24 19:57:37
OOP
Frip, 2018-06-24 19:57:37

The meaning of the interface (not GUI) and why is it needed at all?

Hello!
I read a couple of topics, and as a result, based on what I read, I concluded that the interface is needed for a kind of mandatory class framework that implements this same interface.
Those. creating a class and implementing an interface, the developer must define the fields/properties/methods of the interface in the class.
But I'm wondering if this solution is worth it. What prevents us initially at the stage of class planning from including certain fields / properties / methods in them without resorting to interfaces?
Only one variant of expediency comes to mind, that development is carried out by a group of people, and just like that, the interface obliges the designers of various classes to adhere to a certain standard in the class structure.
Those. I can conclude that the interface, in essence, is intended rather for convenience and organization in the structures of various classes, i.e. to give them something in common, but implemented in each in its own way, at the same time, to oblige to implement exactly everything that is indicated in the interface.
I hope the question is clear, and maybe I answered it myself, but still, I want to be sure that my conclusions are correct so that in the future I don’t get into an absurd situation.
If my arguments are wrong, I ask you to correctly explain the meaning and scope of interfaces.
For example, there is an interface that describes weapons (damage, damage type, ammo volume, ammo type), and there are classes that describe different types of weapons, but at the same time they must have this interface for their own description.
Thank you!

Answer the question

In order to leave comments, you need to log in

7 answer(s)
G
Griboks, 2018-06-24
@Griboks

The interface is another level of abstraction. This is more advanced programming at the level of what to do, not how to do it.
How to use the library? How to link two programs, two different pieces when? How to build flexibility into a project? How to foresee the modernization of the program? You have to use the interface.

G
GavriKos, 2018-06-24
@GavriKos

One common use case is to use an interface as a generic data type for different classes.
For example, there is an interface "Shape", which has two methods - "calculate area" and "calculate perimeter".
There are classes that implement this interface - square, circle, triangle, trapezoid.
And somewhere you need to store something like "current figure" in meaning - so you can use "Figure" as a data type.

#
#, 2018-06-24
@mindtester

I'm not ready to give an assessment of your understanding of OOP ...
but along the way, it's time to tumble in the functional paradigm ... (it definitely won't get worse)
ps interface - an alternative to multiple inheritance. something like this

P
Peter, 2018-06-24
@Morpheus_God

A simple example. You have some kind of program that should send a report. Today you have implemented a class with saving to a file, and tomorrow you will need to send this report in a different way. And each time you will write a new class rewriting the logic of the program? What for. When an interface is created and when passing the report somewhere, the interface method will be used. But how it will be implemented is the tenth thing. The main thing is that the method for transferring the necessary data will be clearly defined at the expense of the interface.
An interface is a clear set of methods for an implemented class or structure that is required in a program. And it's not about the programming team.
Here is one of the patterns in which interfaces are used, where a specific method is called and a completely different part of the program is responsible for its implementation.

M
MrDywar Pichugin, 2018-06-24
@Dywar

Put + many, and add.
Read about SOLID , letter D.
You don't have to do anything just because others do it. But you want to understand this if you want to write not home pet projects, but slightly more complex software that many people will support for years.
Inversion and polymorphism is a very important topic in software development and OOP. Many patterns, principles are tied to this.
GOF, SOLID, GRASP.
You can't even write tests without interfaces.
All this was not invented to fool your head, it is the result of the work of many programmers who have been coming to them on their own for decades.

M
Maxim Fedorov, 2018-06-25
@qonand

Those. I can conclude that the interface, in essence, is intended rather for convenience and organization in the structures of various classes, i.e. to give them something in common, but implemented in each in its own way, at the same time, to oblige to implement exactly everything that is indicated in the interface.

This is the job of an abstract class, not an interface. An interface is primarily intended to describe the interactions between a class and client code. It describes the functionality that the class must have in order for the client code to work correctly.

N
nexus478, 2018-06-25
@nexus478

Those. I can conclude that the interface, in essence, is intended rather for convenience and organization in the structures of various classes, i.e. to give them something in common, but implemented in each in its own way, at the same time, to oblige to implement exactly everything that is indicated in the interface.
I hope the question is clear, and maybe I answered it myself, but still, I want to be sure that my conclusions are correct so that in the future I don’t get into an absurd situation.
If my arguments are wrong, I ask you to correctly explain the meaning and scope of interfaces.

Your conclusions are incorrect. The bottom line is that an interface is needed not for a convenient description of some class, but primarily for the client of this class, as well as the context in which the client uses the interface. When you create an interface, you should think not about the classes that implement it, but about the classes that will use this interface.
The interface allows you to switch between implementations when the context changes, while the client of the interface does not depend on this context, we can say that the interface allows you to fence yourself off from a changeable context. And the implementation commitment is required to ensure that the client of the interface gets the data it needs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question