When I last did a lot of Java programming, I'd do this:
- All classes in a package would be default (package) visibility.
- Each package would have a public PackageNamePack factory class, e.g.
BusinessPack
. - And the package would have a number of interfaces.
- The package factory would have methods like newBusiness(...) which would instantiate new
BusinessImplWhatever
objects, cast as aBusiness
interface.
The factory class:
package io.eaternet.adapters.framework;
public class FrameworkPack {
public static Business newBusiness(String name, String address, String city, String zipcode, String origKey) {
return new BusinessImpl(name, address, city, zipcode, origKey);
}
public static Inspection newInspection(String origKey, String businessOrigKey, String score, String date) {
return new InspectionImpl(origKey, businessOrigKey, score, date);
}
}
An interface:
package io.eaternet.adapters.framework;
public interface Business {
public String getName();
public String getAddress();
public String getCity();
public String getZipcode();
public String getOrigKey();
}
A package-visibility implementation class:
package io.eaternet.adapters.framework;
class BusinessImpl implements Business {
String name, address, city, zipcode, origKey;
BusinessImpl(String name, String address, String city, String zipcode, String origKey) {
this.name = name;
this.address = address;
this.city = city;
this.zipcode = zipcode;
this.origKey = origKey;
}
public String getName() { return name; }
public String getAddress() { return address; }
public String getCity() { return city; }
public String getZipcode() { return zipcode; }
public String getOrigKey() { return origKey; }
}
I've started a new Java library which follows this pattern, and am wondering if it still make sense. This particular code is part of a CSV parser, which is why everything is a string.