Java - Naming of interfaces and their implementations

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I've created a couple of services and dao classes named XXXService or XXXDAO, also packaged away in service and dao packages.

I'd like to extract an interface for the services and dao's but I'm wracking my brain on what to actually call them.

A bit of googling seems to suggest that it's common to think the following as bad practice

- Naming classes eg. SomethingDAO, just put said class in a dao package and omit the dao.
- Naming interfaces ISomething
- Naming interface implementations SomethingImpl

For example I have a service that is responsible for using a DAO to do CRUD operations on a List class so I've called it ListsService and ListsDAO. Now that I'm thinking about creating interfaces from these my first thought was ListsService, ListsServiceImpl and ListsDAO and ListDAOCouchbaseImpl.

Thoughts?
 
Soldato
Joined
23 Feb 2009
Posts
4,976
Location
South Wirral
You can follow that naming convention. Key thing is consistency.

I would ask first though - are you definitely going to create multiple instances of the interface ? Its scary how often I see this stuff massively over-engineered right at the start. Its a quick route to wasting silly amounts of time on boilerplate code, instead of solving the real problem.

My personal rule of thumb is don't bother extracting interfaces out until you have 2 or preferably 3 implementations of them. Its only then you really understand what's common and what's specific.
 
Last edited:
Associate
Joined
7 Nov 2013
Posts
255
Location
Kent, England
Java interfaces should NOT begin with the I prefix, they should be named for the behaviour they describe. As peterwalkley says, the key thing is consistency and if you are inconsistent with the largely adopted Java conventions it will throw other developers off.

For your example, given the interface ListsDAO I would have an implementation such as CouchbaseListsDAO (try and be a bit more specific, lists of what?).

Some have said interfaces are only useful once you have multiple implementations, if you plan to have unit tests for your code, you really should use interfaces which will subsequently allow you to mock functionality.
 
Last edited:
Back
Top Bottom