Martin Probst's weblog

BlogImpl implements Blog

Tuesday, May 20, 2008, 15:27 — 3 comments Edit

There’s a weird thing you’ll see all over the place in Java frameworks and applications of all kind. One might call it Implitis.

The symptoms are that each and every class implements some interface, and it’s always the sole implementor of the interface. So you have a Blog and a BlogImpl, a Window and a WindowImpl, and so forth.

While in some cases there may be reasons for this pattern (e.g. Java’s limited visibility rules force you to make some methods public that really shouldn’t be), I think in many cases this is just someone who has read some book, and now wants to decouple everything and everyone.

If there is only one thing that implements your Foo interface, and you can’t even give it a better name than FooImpl, I declare this a code smell. If the class is conceptually any different from it’s interface, there should be a qualifying name addition for that, like a DatabaseFoo vs. a FileFoo, or a MailFoo vs. an HTMLFoo. If not, you’re probably just complicating you application for no apparent reason.

I was reminded of this by the Spring criticism, see last post. This really ticks me off, people talking with glazing eyes off decoupled everything (do they realize that decoupled means no connection whatsoever? how does that work then?), and in practice only writing horribly complicated, ugly, implitis-infected code.

There is one simple reason for telling people to write an interface for EVERY class. This keeps poor developers from implementing and using a lot of static methods and other crappy stuff.

I’m pleading guilty of using FooImpl from time to time. FooImpl says: “I am the first and very simple implementation. There is maybe a 5050 chance that you will implement another class for my interface.”

Usually when I use FooImpl, I write the simple implementation first and generate interface Foo from that. I may write a next class implementing Foo, but in this case I usually rename FooImpl to something better suited.

I see the awkwardness in this, but I feel like this is typical Java.

And what JP mentions, the I-prefix for interfaces. This is something that makes sense to me, but I rarely do it. Maybe I should try to get used to this.

Have a nice day,


Richie: I don’t really like forcing developers into certain style guides. Sometimes it appears to be necessary - but if you have really bad developers, you’re code base is going to be a mess anyways.

And you’re right, it is typical Java. But I think I could have a lot more fun writing Java if people wouldn’t do this stupid anti-pattern stuff all the time.

I certainly would have a lot more fun if I would be allowed to write Ruby. But I guess, than some of my fellow developers would mess up much more than they do anyways.

And I came up with another reason: J2EE / JEE, JMX and so on teach you more or less to write an interface and exactly one implementation. Some of these frameworks force you to use a certain namescheme for interface and implementation, sometimes it’s a convention. None of them use FooImpl but this style is encouraged through these frameworks and some books.

I’m not saying using FooImpl all the time is a good thing, but there are much bigger problems out there.