T
T
tester2019-20202021-01-06 15:25:46
Java
tester2019-2020, 2021-01-06 15:25:46

How to properly connect different interfaces?

Could you tell me how to implement such tasks correctly?

For simplicity, I will give an example:
There are two self-sufficient interfaces

interface Collection {}
interface Grid {}


There is some implementation of each of them
class PeopleCollection implements Collection {}
class PeopleGrid implements Grid {}


The PeopleGrid class displays data about People, PeopleCollection contains this data, but the structure of the fields in these classes is different. To connect them together, I decided to add an interface and also some implementation
interface GridAdapter {
  void setGrid(Grid g);
  void setCollection(Collection c);
  void prepareCollectionToGrid();
}

class PeopleGridAdapter implements GridAdapter {}


That. I get that for me (well, by name))) it’s clear that the PeopleGridAdapter should interact with the PeopleGrid and PeopleCollection, but if, for example, I add the CityCollection implementation, then the situation in the client code is quite real The
PeopleGridAdapter.setCollection(CityCollection c);
program will execute without errors, but purely logically - some crap turned out and the result will not be what is expected. The names of the classes are conditional and it is clear that instead of PeopleGrid it was possible to name the TableGrid class and write different GridAdapter implementations for different collections, but the problem remains - you can substitute any Collection implementation into any GridAdapter implementation and some kind of crooked logic has turned out

1. How to connect interfaces correctly in general and where in the project should such connections be stored - Grid is a separate package, Collection is a separate package, but where should GridAdapter be?
2. Tell me some books that not only say that you need to program at the interface level, but there are examples of such implementations with more than 2 interfaces

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2021-01-09
@Mercury13

Here it is best to use template classes.

interface Collection <T> {}
class PeopleCollection implements Collection <Person> {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question