Specifically: how come hardly any UI frameworks define their widgets in terms of interfaces, as opposed to abstract base classes? I listed off a dozen mainstream UI frameworks that do this, and was fortunate to get dozens of people responding with their thoughts. The key outcomes were:
- Some UI frameworks (albeit a minority) are interface-based. Examples include ItsNat, Vaadin
- Other frameworks have considered being interface-based (Swing), but on balance decided to use abstract classes. Neither is optimal: "when I was working on the Swing team at Sun Microsystems back in the early days of Java. Some people on the team were of the opinion that Component should be an interface and others that it was best as an abstract class. But really, neither sounded like a good choice"
- Abstract base classes limit future use cases. This is dangerous because "anything of strategic importance will be used in ways that the original designer cannot anticipate"
- The problem with a 'monolithic' interface is that it is cumbersome to evolve: "You will soon have IComponent, IComponent_v2, IComponent_v3"
- The problem with 'granular' interfaces is that you often have to pass around many of them together: "I found it to get problematic as I'd pass around something like (Sizable, Locatable) and then deep in the heart of the framework I realised I needed Focusable also, and I then had to go back up the stack chain and add it to all parameters"
- Java does not have a good way to bundle several 'granular' interfaces together, but other languages may (Scala traits): "It's not yet possible to write Component the way I really want to write it. To do that, you'd have to change Java."
1 comments:
"I found it to get problematic as I'd pass around something like (Sizable, Locatable) and then deep in the heart of the framework I realised I needed Focusable also"
Pass IComponent and when you need Focusable ask for this interface with a simple instanceof, if returns false then throw an Exception with something like "Focusable interface" required, is not perfect but it works.
Post a Comment