Saturday 15 February 2014

java - How can I group static methods in a class in a clean way -



java - How can I group static methods in a class in a clean way -

i expose bunch of general usage static methods in jsf in application scoped bean. code follows:

@applicationscoped @named public class staticutils{ public static void bla(){ ... } public static void blabla(myparam param){ ... } public static void bla2(){ ... } public static void blabla2(myparam param){ ... } ... }

the thing bean growing much , have methods don't have each other. hence thinking of grouping them functionality , extracting them other classes. afterwards would, composition, have instance of each class in aforementioned applicationscopedbean , access static methods grouped through wrapper method.

public class a{ public static void bla(){ ... } public static void blabla(myparam param){ ... } ... } public class b{ public static void bla2(){ ... } public static void blabla2(myparam param){ ... } ... } @applicationscoped @named public class staticutils{ public static void bla(){ a.bla(); } public static void blabla(myparam param){ a.blabla(param); } public static void bla2(){ b.bla2(); } public static void blabla2(myparam param){ b.blabla2(param); } }

as multiple inheritance straight not available in java, , neither want utilize multilevel inheritance nor @applicationscoped resulting beans, there better/cleaner/less verbose way it? can automatically expose public methods inner objects in class, in such way can create code more or less short this?

@applicationscoped @named public class staticutils{ private a; private b b; ... }

any ideas? in advance

the short reply is: no, can't. there several things however:

you utilize 1 interface every functional class , have both a, b or whatever , staticutils implement each of these interfaces. in staticutils class can have like

public static void bla() { a.bla(); }

if these functions static can utilize static imports, no need bean:

import static org.bla.a.bla; import static org.bla.b.*; // etc. you want utilize beans have several (a, b, ...) rather staticutils class. if functions not connected cleanest solution. you can predrag maric suggested , hold instances of a, b , on public members in staticutils class.

java static-methods

No comments:

Post a Comment