java - If the constant interface anti-pattern is such a crime, why does Swing do it? -


i making swing application, , realized had handful of classes needed access same set of constants. couldnt bring myself declare 1 primary holder of them , place them in there , have others reference it; thought, hey, i'll have them inherit common place, java doesnt multiple inheritence, can put infinity interfaces on things. idea came me dump them interface (it's true, naturally occurred me without doing research).

i later learned heresy. "in fact, it's such bad idea there's name it: constant interface antipattern" - as discussed here (along alternate solution (which opted employ)).

i fine until looking @ source code jdialog , jframe, read thusly:

public class jdialog extends dialog implements windowconstants,                                                accessible,                                                rootpanecontainer,                                transferhandler.hasgettransferhandler { ... 

and

public class jframe extends frame implements windowconstants,                                              accessible,                                              rootpanecontainer,                              transferhandler.hasgettransferhandler { ... 

maybe it's me, sure see constant interface in there. more interesting 1 of author declarations in jdialog, i.e. james gosling. father of language allowed alleged mistake on watch?

(another notable example - swingconstans)

if constant interface antipattern such bad idea, why heavily employed in 1 of famous packages of language (i.e. swing)?

the better solution of using static import not available before java 5. point, abusing interfaces import constants considered acceptable because there no better alternative. once have decided jdialog implements windowconstants (and asserted in docs), cannot undo without breaking backwards compatibility. example:

jdialog d = new jdialog(); int flag = d.dispose_on_close; 

while not style, not unusual , break if change jdialog use static imports instead of implementing windowconstants.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -