Re: abstract static methods (again)
On Mon, 19 Oct 2009, Andreas Leitgeb wrote:
Eric Sosman <Eric.Sosman@sun.com> wrote:
Andreas Leitgeb wrote:
I still see some merit in being able to enforce that any concrete
class implementing some thusly declared interface had to offer some
particular c'tor, as a means to help developers of such classes to
not forget about it.
Here's my objection: Suppose there's an Entertainer interface
(or abstract class) and ...
...
Okay, it might make sense for the class of Comedians to have a
default stale Joke (a faithful model of reality, perhaps), ...
:-)
...
The author of Entertainer, who knew nothing about the wants and needs
of those who would come later, ...
Thanks for the entertaining example, but I think it's beside the point.
This type of argument "it's bad for this exemplary usecase, so it must
be bad for all usecases" is obviously flawed. (or was a joke, itself)
On second thought: If the Entertainers were designed to be dynamically
loaded by name, then Comedians just wouldn't have any chance of a individual
default joke. They could offer their Joke- constructor, but unless they
also offered a no-args one, they just wouldn't ever be successfully engaged.
This whole topic is inspired by dynamic loading of classes. Otherwise, there
wouldn't really be any use for dictating constructors at all. Dynamic loading
of classes seems to me of increasing importance with all those AppServers,
J2EE, ... Demanding the default-constructor (or even with a specific set
of arguments) for those classes imposes no new restriction, just formalizes
the restrictions that were already imposed by documentation and use.
This problem was solved in the golden age of the Patternists. The solution
is that you don't load Entertainers, you load EntertainerAgencies.
public interface EntertainerAgency {
public Entertainer hire();
}
String agencyName = loadFromWherever();
EntertainerAgency agency = (EntertainerAgency)Class.forName(agencyName).newInstance();
Entertainer e = agency.hire();
The question of some putative Entertainer constructor goes away. You do
have the question of the EntertainerAgency constructor, but this is much
less pressing. If it really worries you, use a EntertainerAgencyFactory.
Before you ask, yes, it's factories all the way down. Until you reach the
turtles.
tom
--
Ideas are bulletproof. -- V