Unused Interface Settings

I have an interface that is implemented by thirty specific classes. Concrete performers are divided into two groups: each group is inherited from a common abstract class. Abstract classes are defined by constructors for specific performers, including the transfer in the database connection object for each β€œside” of two sides (they have different databases, among other differences).

All current interface methods have several parameters necessary for specific classes to "complete the task", but not all of them are used in each developer.

When I came to add a new method to the interface this morning, I realized that connecting to the database would be necessary only for one of the specific executors, but otherwise it would not be needed. So, this will interest me, should I pass it as a parameter? This is necessary to "complete the task", but only for one of the specific classes, and this class already has a database connection. If I passed the database connection as an interface parameter, the remaining 29 classes will not use it.

What is a good drawing line for an acceptable interface parameter? Any reading / content on this subject I will also be happy to go.

+4
source share
4 answers

For all existing methods of the interface, several parameters are required for specific classes "perform the task", but not all are used in each executor.

It sounds to me just like an interface is gradually turning into a little "divine interface . " Check this out by asking yourself a couple of questions:

  • Is the interface the embodiment of one behavioral concept in your model, or has it become a little convenient dump for method signatures from several concepts? Can it be called something like, for example, Serializable , or more precisely be called SerializableAndSomethingElse .
  • Could you bring the interface to several more connected interfaces and implement 30 different objects with just the ones they need?

When I went to add a new method to the interface this morning, I realized that only one of the specific executors would need to connect to the database, but he would not need the rest. So what makes me wonder if I should pass it as a parameter?

No. In fact, if only one of the executors needs a connection to the database, then it does not look like it belongs to the interface at all. The interface should represent an abstract API, where, as it seems, a database connection is part of the implementation of this API.

+2
source

If this is not part of the abstraction, then this should not be in the interface. And if it is used only by 1 out of 30 implementing classes, then it is definitely not part of the abstraction.

I quickly did a google search for "api design" and the first hit was:

Joshua Bloch presentation slides .

Its points relevant to your question:

  • "When in doubt, leave it."
  • 'Don't let implementation details leak into the API
  • β€œAPI design is tough,” but also β€œAPI design is a noble and useful craft.”
  • "Expect Mistakes"

It sounds like you have a difficult problem - but good luck!

+1
source

It looks like you are following an implementation driven project, as opposed to the case used. You yourself can answer some of these questions by looking at the perspective of the caller. I have more details in this blog post:

http://theamiableapi.com/2011/08/29/considering-the-perspective-of-the-caller/

Greetings

Ferenc

+1
source

The constructor arguments for your various classes must be the co-authors (or configuration values) used in the processing. This is how . They can vary for 30 different implementations. If a database connection is required for some, and not for others, then supply it only as an argument to the constructor.

The interface then forms the basis for processing. This is what .

You should aim for an interface where the API name, arguments, and methods are at the same level. Constructor arguments are likely to be at a lower conceptual level.

0
source

All Articles