John Threepwood
John Threepwood

Reputation: 16143

Conventions of grouping classes in packages

I was always wondering which of these approaches is the best one:

1)

package: cars.blue classes: CarOne.java, CarTwo.java

package: cars.red classes: CarOne.java, CarTwo.java

2)

package: cars.blue classes: BlueCarOne.java, BlueCarTwo.java

package: cars.red classes: RedCarOne.java, RedCarTwo.java

3)

package: cars

classes: BlueCarOne.java, BlueCarTwo.java, RedCarCone.java, RedCarTwo.java

Approach 1 : splits the car classes into different packages.

Approach 2 : seems to be a bit overloaded since the car classes can already be distinguished by the package. But the prefix is an advantage when you just look at the class name, and no second look is needed to see where it belongs to.

Approach 3 : throws all cars in one package.

Which approach is the best one ?

Upvotes: 2

Views: 279

Answers (5)

Michal
Michal

Reputation: 2084

I think it depends what you want to achieve but you should remember that package it's not only for grouping stuff. But you can also take advantage of access modifiers.

If all cars classes should have access to some method they should be in one package so potion 3 is good.

package cars;  
public class BlueCarOne {
  void method() {}
}

package cars;

public class BlueCarTwo {
  void method() {}
}

package cars;

public class RedCarOne {
  void method() {}
}

package cars;

public class RedCarTwo {
  void method() {}
}

In this case only classes from cars package will be able to call method().

But if you want hide some implementation details between RedCar and BlueCar put them into different packages. And mark methods or fields which should be hidden as package protected.

Cheers, Michal

Upvotes: 2

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

Otherwise I would have model/pojo/domain package, an do something like :

package model;

class Car {
    private String carColor; //Blue, Red
    private String carType;  // One, Two

    // getter and setters

}

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Approach 1 is useful mostly when the two packages are exclusive of each other, i.e. in cases when the cars have a common interface, the reds and blues are compiled into separate JARs, and only one of these two JARs is present at a time. When both packages could be present, this naming is confusing.

Approaches 2 and 3 are nearly equivalent, because the names are sufficiently different from each other. Creating a separate package (approach 2) may or may not be an overkill, depending on the way the two groups of classes are used in your application.

Upvotes: 1

Dirk
Dirk

Reputation: 1903

Probably you should create packages encapsulating functional blocks. The distinction of color doesn't really imply any functional difference, thus I would also go with option 3 here.

Upvotes: 1

kosa
kosa

Reputation: 66637

I would go with approach 3. Because all cars are in same package (all domains classes together). Debatable question and everyone may have their own interest. AFAIK there are no pre-defined conventions for these except use case based judgement.

Upvotes: 4

Related Questions