Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Service s = new ServiceImpl() - Why Are You Doing That?

DZone's Guide to

Service s = new ServiceImpl() - Why Are You Doing That?

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

I see the following "best practice" over and over again in projects and reviews :

Service service = new ServiceImpl();

Basically: for every interface there is an implementation with the "Impl" ending. That's not enough: sometimes both are configured in a XML-File (probably to emphasize the importance of this pattern :-)). That is not only bloat (and the opposite of "Convention Over Configuration" idea), but it causes real damage:

  1. Imagine you get another implementation (thats the whole point of an interface) - how would you name it?
  2. It doubles the amount of artifacts - and significantly increases the "complexity"
  3. It is really funny to read the javadocs on the interface and the impl - another redundancy
  4. The navigation in the IDE is less fluent


I asked why developers are writing such code, but I didn't get a reasonable answer, except "because of mocking".
For several years however, this is no longer true. Any ideas, why are you doing that?

From http://www.adam-bien.com/roller/abien/entry/service_s_new_serviceimpl_why

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}