StimVinsh / xdocreport

Automatically exported from code.google.com/p/xdocreport
0 stars 0 forks source link

Do not use Interfaces to define or export constants #42

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
See "Effective Java" from Joshua Bloch Item 17 or Item 19 of second edition. 

Use Interfaces only to define types and respect the "is a" rule.

If Contants are used by more than one class, rather use a final class to define 
those constants and reference it where used. Declare those classes "default" 
(package private) to hide as much as possible of the implementation details not 
required to be known in th API definition.

Inheriting Constants from an Interface bring them down the whole inherit chain 
to classes which may not need them at all, which I find additionally ugly.

Beside, enum are in a lot of cases the better constants. For instance, with the 
following snippet, you have localization for free. Content of an enum used by 
JSF 2 Select-List:

private static List<SelectItem> statList;

    public static List<SelectItem> getItemList(){
        return statList;
    }

    public static String getLocalName(Status actual) {
      ResourceBundle rb = ResourceBundle.getBundle("enumKeyWords");
        return rb.getString(actual.name());
    }

    static {
      ResourceBundle rb = ResourceBundle.getBundle("enumKeyWords");
        List<SelectItem> tmp= new ArrayList<SelectItem>();
        Status[] s = Status.values();
        for (int i = 0; i < Status.values().length; i++) {
            String tag = rb.getString(s[i].name());
            SelectItem se = new SelectItem(s[i], tag);
            tmp.add(i, se);
        }
        statList = Collections.unmodifiableList(tmp);
    }

Original issue reported on code.google.com by juerg.br...@gmail.com on 3 Oct 2011 at 8:35

GoogleCodeExporter commented 8 years ago
Hi Juerg

Thank a lot for your feedback. I will change that as soon as possible.

Regards Angelo

Original comment by angelo.z...@gmail.com on 3 Oct 2011 at 2:38