Tuesday, September 2, 2008

Replacing packages with tags

How can we use tags, and a tagarchy, in Java, if Java is so package oriented? The solution is to map the tagarchy onto the package hierarchy, much in the same way as persistence layers try to map objects into database tables. Unfortunately, just as there is an object-relational impedance mismatch, there will be a tag-package impedance mismatch too, but tools can help to make this impedance mismatch transparent to the developers that want to use a tagarchy rather than a hierarchy.

Here's a practical way to map tags to packages:
  1. For each class, order its tags alphabetically, and concatenate them with a dot («.») inbetween each tag.
  2. Prefix the result from the first step with what would otherwise be the top level package name.
To make things clear, here's an example. Suppose we have a class named RegisterOrderDialog. It is tagged with the tag order because it belongs to the order feature, and it is tagged with the tags ui and swing because it belongs to the SWING-based user interface. Ordering the three tags alphabetically results into order.swing.ui, which we'll prefix with the name com.acme.store, the company name and the project name. The package name for the class RegisterOrderDialog should therefore be com.acme.store.order.swing.ui.

The project will also contain classes tagged with e.g. ui and html for the web based user interface, and domain for the domain objects, and classes tagged with e.g. bill and admin for the billing feature and the administration feature. This may result in the following list of packages present in the project:
com.acme.store.admin.domain
com.acme.store.admin.html.ui
com.acme.store.admin.swing.ui
com.acme.store.bill.domain
com.acme.store.bill.html.ui
com.acme.store.bill.swing.ui
com.acme.store.domain.order
com.acme.store.html.order.ui
com.acme.store.order.swing.ui
Notice that in this example, most of the packages have the feature tag first, but there are two packages that have the layer tag first.