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

I was making a swing application and realized that I have several classes that need access to the same set of constants. I could not bring myself to declare one of the main holders of them and place everyone there, and others would talk about it; I thought, hey, I just all inherit from some common place, but Java does not do multiple inheritance, BUT I can put infinite interfaces on things. So, an idea came to me to dump them all into an interface (it's true, this is just a natural event for me, without doing any research).

Later I found out that it was heresy. "In fact, such a bad idea that there is a name for it:" Antipattern Permanent Interface "- as discussed here (along with an alternative solution (which I chose to use)).

I was fine with this until I looked at the source code for JDialog and JFrame , which reads as follows:

 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 just me, but I'm sure there is a consistent interface there. Even more interesting was one of the author's declarations in JDialog, i.e. James Gosling Did the tongue father make this supposed mistake on his watch?

(Another famous example is SwingConstans )

If a persistent antipattern interface is such a bad idea, then why is it so heavily used in one of the most famous language packages (i.e. swing)?

+6
source share
1 answer

The best solution for using static import was not available until java 5. Until then, the abuse of interfaces for import constants was considered acceptable, because there was no better alternative. Once you decide that JDialog implements WindowConstants (and claimed this in the docs), you cannot undo it without breaking backward compatibility. Example:

 JDialog d = new JDialog(); int flag = d.DISPOSE_ON_CLOSE; 

Although probably not a very good style, it’s not so unusual and breaks if we change JDialog to use static imports instead of implementing WindowConstants .

+6
source

All Articles