A
A
Alex Krynytskyi2020-09-07 14:47:18
Angular
Alex Krynytskyi, 2020-09-07 14:47:18

Why do we need interfaces in Angular?

Why are interfaces needed in Angular?
Can you help with a visual guide or video tutorial?
to put everything on the shelves?

And from what I found everything is somehow blurry and not very clear

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Yakimchuk, 2020-09-07
@NEOmanceR

It's not about Angular, it's about what an interface is. I will tell from my practice.
An interface is an interaction contract between technical entities (classes, objects, functions, and so on). If we create 10 objects that respect (implement) the interface, then they can be replaced with each other (just don't violate LSP, this is the SOLID principle).
So, from here there are powerful bonuses:
First, if you create interfaces, then the entities begin to depend not on each other, but on the interface. This means that instead of one class depending on another concrete class, you can use an interface dependency and any class that implements the interface. Thus, if necessary, you can safely replace one class with another, if they implement the same interface. This is called the dependency inversion principle (one of the SOLID principles).
It turns out that the entities are not tied to each other, and they can be developed independently of each other, and in development, the fewer connections between classes/objects, the better. If later you need to change the code, change the class, there will be much less problems. Less, because if there is a chain of dependencies, then one cannot be changed, because the other will break, and the entire chain will have to be changed. By removing direct dependencies, changing the code becomes easier.
The second is stability, for me personally. I work on TypeScript, and there you can describe the interface, the signature of the function, and expect any data that implements it as input, and I don’t care where they come from, but as long as they implement the interface, everything will work stably.
Third, it is an understanding of what you are working with. When there is an interface, you always understand what can be used for an object, what is there, and what type it is. Just very convenient.
Well, the most important thing, for me personally, is that we are engineers, i.e. we have to design the system and then implement it, and the interface is one of our blueprints.
You first think about how it will work, what data is there, then you describe it through interfaces, and so on, and then you implement it. Therefore, we can consider that the interface is one of the key building blocks of the application architecture. Without it, entities will try to pull the data they need from the objects at the input, without understanding whether it is possible to pull them from there at all, what really is there at all, and as a result, chaos will result.
Therefore, Angular has nothing to do with it, it's just a framework, and the design principles there are the same as in any other application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question