I saw this article on Reddit that applies principles from human-factors design to API design. I think that’s a great idea, but this suggestion sounds weird to me.
Now we could use progressive disclosure to help reduce the complexity of that JButton class: put the expert stuff in an object returned by a getExpertKnobs() method, and the graphics subsystem hooks in an object returned by a getIntegrationHooks() method, and you would be left with a button API that had just a handful of methods—the basic methods we all need.
This brings the number of methods in a button from about 100 to about 10, which sounds great until you try to subclass. Overriding a method that’s in the object returned by the getExpertKnobs method would be a little tricky. It’s likely that you don’t even know the real class of that object because those methods are likely to return references to interfaces, plus the component code is in charge of constructing the objects, so there would have to be a way plug into that too, so that your subclass is constructed. I agree that the interface would be much simpler for most users, but it is much more complex for subclassers.
There’s also the issue of what would be in the expertKnobs object. Some of the methods in JButton that would go in there actually come from JComponent or Component. In fact, as we progressively go down the class hierarchy, we are adding methods into the class that might belong together, which means that there is probably an expertKnobs class for each component class. But, the getExpertKnobs() method is probably introduced early in the hierarchy — what does it return? Will I have to downcast to get to the button’s expert knobs?
I don’t have a solution — but these issues probably drove the design of JButton more than the idea that they just sloppily added a 100 methods without giving a thought to the complexity of the class.
But the point is taken, and I like the idea of using hard won knowledge from other domains in software design.
Another recent blog on this topic was Steve Hawley’s Principle of Least Astonishment essay — the last topic, on layering APIs, has his approach to this problem.