I have a base class which is abstract, and various concrete
implementations of it that might be chosen in different circumstances
(different OSes, different modules installed). I'd like a "magic
constructor" that attempts to find "the best" subclass... Problem is,
there's no easy way to answer what is best...

My current best ideas are either:

  a: Each module just gives a ranking number, and we find the one with
     the highest ranking.
     
  b: Each module declares a list of other modules it knows it's better
     than...
     
Both of these have problems.

Idea a would be implemented by something like:

   my @modules = [some magic with Module::Pluggable];

   my %rankings = map { $_ => $_->get_ranking } @modules;
   # get_ranking is a method, so it can make a runtime choice on how
   # good it is in the current situation

   return ( sort { $ranking{$b} <=> $ranking{$a} } @modules )[0]

This works, provided every module author plays the same way with regard
to rankings. But how to choose these? Because my module declares 30,
does that mean it's better than yours which is 20, or is it just that we
happened to pick different numbers? Perhaps some guidelines by what is
meant by various numbers - e.g. 10 for the basic default one, 50 for a
per-OS one, 100 for a per-installed-library one, etc...

Idea b would involve taking each module in turn, and removing from the
set of candidates all other modules that it declares itself to be better
than. We will then end up with a (possibly-empty) subset of what we
started with. If we have one, great, use it. If we have multiple, how
can we choose? More's the problem, what if we end up with none? What if
Foo claims to be better than Bar, but Bar claims to be better than Foo?

I suspect approach a may end up being better in practice, even if it
involves a bit more coordination between the authors.

How's anyone else go about this problem?

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Attachment: signature.asc
Description: Digital signature

Reply via email to