What strategy do you use for package naming in Java projects and why?

I thought about this awhile ago and it recently resurfaced as my shop is doing its first real Java web app.

As an intro, I see two main package naming strategies. (To be clear, I'm not referring to the whole 'domain.company.project' part of this, I'm talking about the package convention beneath that.) Anyway, the package naming conventions that I see are as follows:

  • Functional: Naming your packages according to their function architecturally rather than their identity according to the business domain. Another term for this might be naming according to 'layer'. So, you'd have a *.ui package and a *.domain package and a *.orm package. Your packages are horizontal slices rather than vertical.

    This is much more common than logical naming. In fact, I don't believe I've ever seen or heard of a project that does this. This of course makes me leery (sort of like thinking that you've come up with a solution to an NP problem) as I'm not terribly smart and I assume everyone must have great reasons for doing it the way they do. On the other hand, I'm not opposed to people just missing the elephant in the room and I've never heard a an actual argument for doing package naming this way. It just seems to be the de facto standard.

  • Logical: Naming your packages according to their business domain identity and putting every class that has to do with that vertical slice of functionality into that package.

    I have never seen or heard of this, as I mentioned before, but it makes a ton of sense to me.

  • I tend to approach systems vertically rather than horizontally. I want to go in and develop the Order Processing system, not the data access layer. Obviously, there's a good chance that I'll touch the data access layer in the development of that system, but the point is that I don't think of it that way. What this means, of course, is that when I receive a change order or want to implement some new feature, it'd be nice to not have to go fishing around in a bunch of packages in order to find all the related classes. Instead, I just look in the X package because what I'm doing has to do with X.

  • From a development standpoint, I see it as a major win to have your packages document your business domain rather than your architecture. I feel like the domain is almost always the part of the system that's harder to grok where as the system's architecture, especially at this point, is almost becoming mundane in its implementation. The fact that I can come to a system with this type of naming convention and instantly from the naming of the packages know that it deals with orders, customers, enterprises, products, etc. seems pretty darn handy.

  • It seems like this would allow you to take much better advantage of Java's access modifiers. This allows you to much more cleanly define interfaces into subsystems rather than into layers of the system. So if you have an orders subsystem that you want to be transparently persistent, you could in theory just never let anything else know that it's persistent by not having to create public interfaces to its persistence classes in the dao layer and instead packaging the dao class in with only the classes it deals with. Obviously, if you wanted to expose this functionality, you could provide an interface for it or make it public. It just seems like you lose a lot of this by having a vertical slice of your system's features split across multiple packages.

  • I suppose one disadvantage that I can see is that it does make ripping out layers a little bit more difficult. Instead of just deleting or renaming a package and then dropping a new one in place with an alternate technology, you have to go in and change all of the classes in all of the packages. However, I don't see this is a big deal. It may be from a lack of experience, but I have to imagine that the amount of times you swap out technologies pales in comparison to the amount of times you go in and edit vertical feature slices within your system.

  • So I guess the question then would go out to you, how do you name your packages and why? Please understand that I don't necessarily think that I've stumbled onto the golden goose or something here. I'm pretty new to all this with mostly academic experience. However, I can't spot the holes in my reasoning so I'm hoping you all can so that I can move on.


    For package design, I first divide by layer, then by some other functionality.

    There are some additional rules:

  • layers are stacked from most general (bottom) to most specific (top)
  • each layer has a public interface (abstraction)
  • a layer can only depend on the public interface of another layer (encapsulation)
  • a layer can only depend on more general layers (dependencies from top to bottom)
  • a layer preferably depends on the layer directly below it
  • So, for a web application for example, you could have the following layers in your application tier (from top to bottom):

  • presentation layer: generates the UI that will be shown in the client tier
  • application layer: contains logic that is specific to an application, stateful
  • service layer: groups functionality by domain, stateless
  • integration layer: provides access to the backend tier (db, jms, email, ...)
  • For the resulting package layout, these are some additional rules:

  • the root of every package name is <prefix.company>.<appname>.<layer>
  • the interface of a layer is further split up by functionality: <root>.<logic>
  • the private implementation of a layer is prefixed with private: <root>.private
  • Here is an example layout.

    The presentation layer is divided by view technology, and optionally by (groups of) applications.

    com.company.appname.presentation.internal
    com.company.appname.presentation.springmvc.product
    com.company.appname.presentation.servlet
    ...
    

    The application layer is divided into use cases.

    com.company.appname.application.lookupproduct
    com.company.appname.application.internal.lookupproduct
    com.company.appname.application.editclient
    com.company.appname.application.internal.editclient
    ...
    

    The service layer is divided into business domains, influenced by the domain logic in a backend tier.

    com.company.appname.service.clientservice
    com.company.appname.service.internal.jmsclientservice
    com.company.appname.service.internal.xmlclientservice
    com.company.appname.service.productservice
    ...
    

    The integration layer is divided into 'technologies' and access objects.

    com.company.appname.integration.jmsgateway
    com.company.appname.integration.internal.mqjmsgateway
    com.company.appname.integration.productdao
    com.company.appname.integration.internal.dbproductdao
    com.company.appname.integration.internal.mockproductdao
    ...
    

    Advantages of separating packages like this is that it is easier to manage complexity, and it increases testability and reusability. While it seems like a lot of overhead, in my experience it actually comes very natural and everyone working on this structure (or similar) picks it up in a matter of days.

    Why do I think the vertical approach is not so good?

    In the layered model, several different high-level modules can use the same lower-level module. For example: you can build multiple views for the same application, multiple applications can use the same service, multiple services can use the same gateway. The trick here is that when moving through the layers, the level of functionality changes. Modules in more specific layers don't map 1-1 on modules from the more general layer, because the levels of functionality they express don't map 1-1.

    When you use the vertical approach for package design, ie you divide by functionality first, then you force all building blocks with different levels of functionality into the same 'functionality jacket'. You might design your general modules for the more specific one. But this violates the important principle that the more general layer should not know about more specific layers. The service layer for example shouldn't be modeled after concepts from the application layer.


    I find myself sticking with Uncle Bob's package design principles. In short, classes which are to be reused together and changed together (for the same reason, eg a dependency change or a framework change) should be put in the same package. IMO, the functional breakdown would have better chance of achieving these goals than the vertical/business-specific break-down in most applications.

    For example, a horizontal slice of domain objects can be reused by different kinds of front-ends or even applications and a horizontal slice of the web front-end is likely to change together when the underlying web framework needs to be changed. On the other hand, it's easy to imagine the ripple effect of these changes across many packages if classes across different functional areas are grouped in those packages.

    Obviously, not all kinds of software are the same and the vertical breakdown may make sense (in terms of achieving the goals of reusability and closeability-to-change) in certain projects.


    There are usually both levels of division present. From the top, there are deployment units. These are named 'logically' (in your terms, think Eclipse features). Inside deployment unit, you have functional division of packages (think Eclipse plugins).

    For example, feature is com.feature , and it consists of com.feature.client , com.feature.core and com.feature.ui plugins. Inside plugins, I have very little division to other packages, although that's not unusual too.

    Update: Btw, there is great talk by Juergen Hoeller about code organization at InfoQ: http://www.infoq.com/presentations/code-organization-large-projects. Juergen is one of architects of Spring, and knows a lot about this stuff.

    链接地址: http://www.djcxy.com/p/84174.html

    上一篇: 为什么`java.lang.SecurityException:禁止包名:java`是必需的?

    下一篇: 您在Java项目中使用什么策略进行软件包命名?为什么?