R
R
random552022-03-31 11:14:02
OOP
random55, 2022-03-31 11:14:02

What interfaces should be created?

Do you think interfaces should be created for the client/task, or should they be as universal as possible, not tied to the context?

example:

interface IPerson
{
    public function move();
    public function eat();
    public function sleep();
    public function work();
}


or

interface IMove
{
    public function move();
}

interface IEat
{
    public function eat();
}

interface ISleep
{
    public function sleep();
}

interface IWork
{
    public function work();
}


The first approach limits the use of the interface to a certain module, because it describes a person, and not individual behaviors. And if, for example, we had a separate IMove interface, then we could implement it in any module / component, for example, in a conditional File, because we can move the file. It's the same with ISleep, you never know what can fall asleep, starting from the operating system or the execution thread, ending with a character in Tamagotchi.

But one thing is confusing, IMove::move() for the character of the game, most likely, will take x,y coordinates, and for the file - the final path.

Is it worth bothering and looking for something in common with completely dissimilar objects and phenomena, so that interfaces can be dumped into one heap in a single directory, or is this an extreme?

UPD: I thought here, if the file and the character of the game are completely different things, then inside the game logic you may need to move along the coordinates not only something that implements Person, but, possibly, some decorations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-03-31
@random55

And if, for example, we had a separate IMove interface, then we could implement it in any module / component, for example, in a conditional File, because we can move the file. It's the same with ISleep, you never know what can fall asleep, starting from the operating system or the execution thread, ending with a character in Tamagotchi.

Wrong. IMove (or Movable would be more accurate) makes sense in the context of some subject area. And moving a file, moving a sprite, moving a 3D object, moving a robot arm are all different movements. Accordingly, the interfaces will be different (located in different modules, even if they are named the same), for example:
filesystem.Movable
graphics.2d.Movable
graphics.3d.Movable
servo.Movable

Therefore, the answer is no, there is no need to invent something common for dissimilar objects and phenomena. Only if this something in common allows you to somehow better model the subject area.
In general, the main principle here is this: the interface should make sense, describe a certain unit of functionality from the point of view of the subject area. For example, if in your subject area the move operation makes sense without the eat and sleep operations, it can be separated into a separate interface (or not, it depends on other factors). If the eat and sleep operations must always go in pairs and each consumer of this interface needs both operations, they must be in the same interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question