View this page "Package by feature, not layer"

21 views
Skip to first unread message

John O'Hanley

unread,
May 11, 2009, 6:45:57 PM5/11/09
to javapractices-dot-com-discuss


Click on http://groups.google.ca/group/javapractices-dot-com-discuss/web/package-by-feature-not-layer?hl=en
- or copy & paste it into your browser's address bar if that doesn't
work.

Christian Beil

unread,
Jun 7, 2009, 9:33:18 AM6/7/09
to javapractices-dot-com-discuss
Hi John,
I like the idea of packging by feature. I have to admit that (probably
like most developers) I usually package by layer. I have a package
where beans reside, a package for the DAOs, one for the services, and
so on. DAO, service and other packages often mostly contain interfaces
and have a sub-package impl for the implementation of the interfaces.
I am willing to restructure the project I am currently working on. But
I have a few questions and I hope you can answer them.
How does package-by-feature combine with programming against
interfaces, should both interfaces and implementation classes reside
in the same package?
Test classes are usually not kept in the same package as the
implementation. But to access the class under test, it has to be
public. Thereby all important classes have to be public anyway and
package privacy is lost. What do you think about this?

John O'Hanley

unread,
Jun 7, 2009, 6:37:30 PM6/7/09
to javapractices-dot-com-discuss
Hi Christian,

Thanks for your question.


Test classes :
I would recommend that they reside in the same package as the class
under
test. I know that this goes against the habit of many programmers -
but I
think that habit is a bad habit.

If you want to remove test classes from production builds, then tools
such
as ANT can easily remove them. For example, ANT can find all classes
that
follow a certain naming style - all classes that start with 'Test'
for
example - and leave them out of the build. Such logic is not hard to
implement with ANT, so I don't see why it shouldn't be used.

Placing tests in a separate directory is just another example of
package by
layer. It's not modular, and breaks encapsulation of items for no
compelling
reason.


Interfaces + implementations :
This one is hard to answer. I don't have the context. Why are the
interfaces
being defined? Are all implementations of the interfaces under your
complete
control?

My default advice would likely be to place them in the same directory.

Regards,
John O'Hanley


Christian Beil

unread,
Jun 9, 2009, 7:29:24 PM6/9/09
to javapractices-dot-com-discuss
Hi John,

Thank you for your answer.

Oh man, I am typing this the second time because I hit the back
button.

The more I think about it, the more I get accustomed to packaging by
feature. Regarding the test classes, I fell back into good old package
by layer. But I quite like the idea that tests go into the same
package as the classes they are testing; they are definetely coupled.
Filtering the test classes using Ant is no problem, you are right.

Concerning interfaces, I meant the ones under my control. We usually
use Spring for dependency injection. So the fields of a class have an
interface type, and concrete implementations are injected. This also
allows to create mocks more easily.
I think I go along with you: in this case interfaces and
implementation classes should go into the same package.
If I wrote an API to a library or framework, it would probably make
sense to put some interfaces into a separate location.

Sometimes it is not that easy to find the actual features. Not every
domain class makes up a single feature. I think package by feature is
also found (at a higher level?) in the modularization that plugin
system like the ones from Eclipse or NetBeans do. They also divide the
application in different feature sets, although not at the package
level.

Since my questions are answered so far and I like the concept quite
much, I will go ahead and give it a try. Thanks for the inspiration.

Do you listen to the JavaPosse podcast? I know that you have your own
survey on javapractices.com, but what do you think of posting a
question to the JavaPosse moderator group. I would like to hear some
more opinions on package by feature.

Regards,
Christian Beil

John O'Hanley

unread,
Jun 9, 2009, 8:18:32 PM6/9/09
to javapractices-dot-com-discuss
Hi Christian,

It's always best to try things out for a while, to see how they work
in practice. If it works for you, use it. If not, that's OK too.

I do follow the Java Posse folks, yes. But I am not a member of their
group, however. If you post something there, let me know, and I will
join in. [I will be taking a short vacation over the next few days.
Back on Sunday.]

Regards,
John

Christian Beil

unread,
Jun 13, 2009, 7:19:32 AM6/13/09
to javapractices-dot-com-discuss
Hi John,

I hope you are enjoying your short vacation.
I posted a question on the JavaPosse's listener feedback and on the
Google group.
http://groups.google.com/group/javaposse/browse_thread/thread/7b66fdb48619f77d

Regards,
Christian
Reply all
Reply to author
Forward
0 new messages