G
G
gitdev2018-03-12 13:10:18
OOP
gitdev, 2018-03-12 13:10:18

Do I need to create interfaces for a single class?

If necessary, in what cases.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
imhuman, 2018-03-12
@gitdev

If there is a possibility that the class may be replaced by another in the future or already several classes with a common interface are connected under different conditions

M
Mercury13, 2018-03-12
@Mercury13

1. If some kind of abstraction can be pulled out of the class. For example, the abstraction "stream" can be pulled out of the "file" object. Private - The Project object implements the Modifiable interface with two functions: modify() and isModified().
2. To simplify unit testing under the condition of ownership.
Suppose we have a class "class" (school) and a class "student". The student knows what class he is in.
In such a situation, a “tangle” is obtained: if you need to make a student, then you need to make a class.
This vicious circle can be broken by making the ISchoolClass interface and deriving a class from it. When unit testing, we replace the class with some kind of stub.

D
ddd329, 2019-05-15
@ddd329

See what class.
If this is the essence of the subject area, then no, it's not worth it.
If these are services, then yes, it's worth it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question