interface

Where Do Interfaces Live?

Sometimes when I review code, I notice that there seems to exist a general lack of understanding of how interfaces and their implementers should be packaged up into components. Components are the unit of deployment. Java produces jar files, Ruby gems, .NET .dlls and with JavaScript, we deploy .js files. 

Note: Not all interfaces divide functionality up into separate components. We only use interfaces in this manner when we aspire to have pluggable components.  

Say, we have an interface, and two classes, an implementer of the interface, and a consumer of the interface. If the classes (interface implementer and interface consumer) are located in different components, then where should the interface go? Should it go into the component with the interface consumer or should we place it into the component that contains the implementer?

Let’s get more concrete with an example. Say, we are developers on a library management system. In this system, we have two components, LibrarySystem.BusinessLogic and LibrarySystem.MongoDb. The business logic component contains a high-level Application class. Class Application uses an interface IDatabase to persist data to a database. At this stage, IDatabase has only one implementer, MongoDbDatabase, which resides in the LibrarySystem.MongoDb component. 

We are conscientious developers and want to create an extensible and pluggable system architecture. 

Question: Into which component should we put IDatabase?

Here are our two choices again:

1.) We put interface IDatabase into LibrarySystem.BusinessLogic,

2.) Or we put IDatabase into LibrarySystem.MongoDb.

Which one makes more sense? And why?

 

Check out the answer and reasoning:  Answer To ‘Where Do Interfaces Live?’

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply