I am looking for a template for the following. (I work in Perl, but I do not think this language is particularly important).
With parent class Foo and kids bar, Baz, Bazza.
One method of constructing Foo is to parse a string, and part of this string will implicitly determine which class should be created. For example, if it launches "http:", then it is a bar, but if it does not contain, but contains "[Date]", then the Base likes it, etc.
Now, if Foo knows about all its children, and which line is a bar, what is Baz, etc., it can call the corresponding constructor. But the base class should not have any knowledge about its children.
I want the Foo constructor to try their children one at a time, until one of them says, "Yes, this is mine, I will create a thing."
I understand that in the general case this problem is not defined correctly, since there can be more than one child who will accept the string, and therefore the order in which they are called matters: ignore this and assume that the characteristics of the string are such that there is only one the child class will love the string.
The best I came up with is that the child classes βregisterβ with the base class during initialization, to get a list of constructors, and then skip them. But is there a better method that I am missing?
Code example:
package Foo;
my @children;
sub _registerChild
{
push @children, shift();
}
sub newFromString
{
my $string = shift;
foreach (@children) {
my $object = $_->newFromString(@_) and return $object;
}
return undef;
}
package Bar;
our @ISA = ('Foo');
Foo::_registerChild(__PACKAGE__);
sub newFromString
{
my $string = shift;
if ($string =~ /^http:/i) {
return bless(...);
}
return undef;
}