P
P
Pavel2012-01-04 01:14:13
OOP
Pavel, 2012-01-04 01:14:13

Why define an interface before implementing a class?

Why do books on many programming languages ​​(C#, Objective-C) first define an interface and then implement a class? Isn't it easier to write a class right away? So after all and the code turns out less.
Why is the interface not a waste of time and lines of code?
Why is it necessary to first describe the interface and then implement the class?

Answer the question

In order to leave comments, you need to log in

11 answer(s)
A
AlexanderG, 2012-01-04
@region23

And tell me what exactly to read? Just an amazing book. Most aspects of software development are covered, from design and requirements to advice on naming variables. Everything is explained in a very accessible way, but at the same time not too abstruse, but not too simplistic either.
image

S
Slayt, 2012-01-04
@Slayt

Try to write a simple program for yourself that will draw different shapes (square, ellipse, rhombus) on the screen.
You will soon find yourself constantly writing:

for (Square s: squares) {
  s.draw();
}
for (Ellipse e: ellipses) {
  e.draw();
}
for (Diamond d: diamonds) {
  d.draw();
}
...

quite inconvenient, especially if you later need to add more shapes. It would be much better to declare an interface common to shapes with the draw() method and use it in the code:
for (Figure f: figures) {
  f.draw();
}

M
Monnoroch, 2012-01-04
@Monnoroch

But it seems to me that reading books is not enough here, otherwise the question will not go away, but will only get stronger. You just need to write programs. When writing programs, you suddenly find that it's easier, better, and more enjoyable.

V
Vladimir Chernyshev, 2012-01-04
@VolCh

By the way, this is not always good practice. You can consider this premature universalization and oopization in many cases, if you are not sure that you will need several classes with different implementations, but one interface (testing is a separate case, largely depends on the language and tools, it is easy to create a mock / stub somewhere without interfaces, somewhere, probably, it is impossible). That's when you need it, then create it, and a reliable assessment of the probability whether you need it or not comes only with experience. But it’s better, IMHO, for faster typing to once again select an interface from an already implemented class than to select it right away, and then not remember that it was in vain to select it and drag it around the entire project without much need. And when the interface is not enough, it is felt, especially in languages ​​with strong typing.

A
Anatoly, 2012-01-04
@taliban

The interface does not need to be described before the class, but it is possible, these are different things. And as mentioned above, you yourself will understand when it's worth it and when it's not, you can't say for sure "do the interface here, but don't need it here."

M
Monnoroch, 2012-01-04
@Monnoroch

Regarding the addition to the question:
The answer to the question "why?" trivial: it's more convenient.
But to understand why you can only from your own experience, it's like why sweet is delicious. No matter how much you explain, you won't understand until you try.

S
szKarlen, 2012-01-04
@szKarlen

I think the first thing to be clear is that it is not always and in all cases necessary to implement an interface. So, for the Point2D class, this is absolutely not necessary, or Vector2D, Vector3D.
Above was an example with shapes - however, it does not show this. Its purpose is inheritance in OOP (recall three postulates: polymorphism, encapsulation and inheritance).
Interfaces, in practice, are defined in most cases for:

  1. reducing the connectivity of the system
  2. classes (objects) that perform the role of data processing
  3. classes (objects) following some programming pattern (repository, strategy)
  4. at the design stage to create stubs
  5. for use in unit testing

And since you also wrote about C #, let's remember about the explicit and implicit implementation of the interface, which is convenient, for example, in the GUI.
The interface is the skeleton on which the entire system is built.

V
VenomBlood, 2012-01-04
@VenomBlood

Well, at least here are a couple of reasons:
1) When designing, you first think what the class should do, and only then how , from here the interface is made first, then the class.
2) For less cohesiveness of the system, you can communicate with the interface wherever possible, and then substitute another class without any problems.
In fact, there are many more reasons, here I advise you to read some books on programming (specifically on programming as such, and not on any language), it is well explained there.

A
Alexander Kouznetsov, 2012-01-04
@unconnected

I was taught that it is needed primarily for teamwork.
Let's say that a certain class receives some set of data from the database. There is no DB yet. Accordingly, we describe the interface and implement a class that stupidly returns a certain set of constants. When the database appears, someone is already implementing a class that receives real data, not stubs. The debugging process is also simplified - it is easier to isolate the code: while class A was used to implement the interface, everything worked, class B became - everything broke.
Something like this.
If you are alone, without an ensemble, and a reaper, and a player on the pipe, then there is much less sense in describing interfaces.

@
@ngreduce, 2012-01-04
_

As an option - the implementation of several interfaces in one class, without using multiple inheritance.

L
Lihonosov, 2012-01-06
@Lihonosov

For tests! JUnit, etc. For example:
Interface for working with the database
1. Implementation of the interface for working with Mysql
2. Implementation of the interface for working with MongoDB
3. Implementation of the interface for working with "In Memory DB"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question