what is the convention to name interface and its implementation class in java?
Interface : ISomeService
Impl : SomeService
or
Interface : SomeService
Impl : SomeServiceImpl
Thanks!
what is the convention to name interface and its implementation class in java?
Interface : ISomeService
Impl : SomeService
or
Interface : SomeService
Impl : SomeServiceImpl
Thanks!
Name your Interface
what it is. Truck
. Not ITruck
because it isn't an ITruck
it is a Truck
. An Interface
in Java is a Type. Then you have DumpTruck
, TransferTruck
, WreckerTruck
, CementTruck
, etc. When you are using the Interface
Truck
in place of a sub-class you just cast it to Truck
. As in List<Truck>
. Putting I
in front is just crappy hungarian style notation tautology that adds nothing but more stuff to type to your code.
All modern Java IDE's mark Interfaces and Implementations and what not without this silly notation. Don't call it TruckClass
that is tautology just as bad as the IInterface
tautology.
If it is an implementation it is a class. The only real exception to this rule, and there are always exceptions is AbstractTruck
. Since only the sub-classes will every see this and you should never cast to an Abstract class it does add some information that the class is abstract and to how it should be used. You could still come up with a better name than AbstractTruck
and use BaseTruck
instead. But since Abstract
classes should never be part of any public facing interface it is an acceptable exception to the rule.
And the Impl
suffix is just more noise as well. More tautology. Anything that isn't an interface is an implementation, even abstract classes which are partial implementations. Are you going to put that silly Impl
suffix on every name of every Class?
The Interface
is a contract on what the public methods and properties have to support, it is also Type information as well. Everything that implements Truck
is a Type of Truck
.
Look to the Java standard library itself. Do you see IList
, ArrayListImpl
, LinkedListImpl
? No you see. List
and ArrayList
and LinkedList
. Here is a nice article about this exact question. Any of these silly prefix/suffix naming conventions all violate the DRY principal as well.
Also if you find yourself adding DTO
, JDO
, BEAN
or other silly repetitive suffixes to objects then they probably belong in a package instead of all those suffixes. Properly packaged namespaces are self documenting and reduce all the useless redundant information in these really poorly conceived proprietary naming schemes that most places don't even adhere to in a consistent manner. If all you can come up with to make your Class name unique is suffixing it with Impl
, then you need to rethink having an Interface
at all. So when you have an situation where you have an Interface
and a single Implementation
that is not uniquely specialized from the Interface
you probably don't need the Interface
.