Overusing Inheritance ?
Java Swing and Java2D rely a lot on inheritance. Most people have told me I should avoid inheritance as much as possible and only use it when necessary. So is the extensive use of Inheritance in Java2D and Java Swing justified?
Adapter Pattern
I have heard a lot of praise for the adapter pattern, and I’ve heard a lot of criticism. What I gathered from all that, though, is that the adapter pattern is only considered good design if used at the right place. Irrelevant use of the adapter pattern causes people reading your code to scowl. Is the adapter pattern correctly and relevantly used in the two Java APIs?
Singletons
Both APIs also tend to use a considerable number of Singletons. Is this good?
The Question
So, Is The Java2D Api and Java Swing a good example of an Object Oriented Programming Interface? Should I use their techniques in my code?
I have no certainty on this, but I can offer some thought:
inheritance: suppose you preffered to use interfaces and composition instead of reuse-by-inheritance in Swing, then you would need to do an awful lot of forwarding (
ComponentandJComponenthave dozens of methods)adapter pattern: see above
singletons: they are appropriate to model parts of a system which are genuinely unique (I would agree that
java.awt.Desktopcould be implemented as a singleton — even though it uses a factory method)overall design: you might opt to judge things by results — Swing is robust, extensible, and widely used.