Is it generally considered bad practice for a base class constructor to return objects in its derived classes depending on input parameters?
What you're describing is often called a "Factory Method", though typically it'll be named something other than "new".
Consider renaming the "constructor", so that an invocation reads something like
$s = Sentence->from_string("This is a test.");
or maybe
$s = Sentence->classify("This is a test.");
Update: Technically, a factor method is an instance method that can be overridden. Using a class method for this is similar, but this might be closer to a Factory Function, or maybe a Builder pattern.
| [reply] [d/l] [select] |
I had a similar thing recently, and funnily enough, in a similar (linguistic) context. One problem comes to my mind: if you return child objects from your parent constructor, the subclasses can have their own methods, but it's not convenient for them to have any object variables, in addition to those they inherit.
Eg, an abstract Word class may have variables english and greek, but its child class, Noun, may need nom_singular, whereas child class, Verb, may need to be constructed with infinitive.
Perhaps at the moment you can't imagine, say, your Interrogative and Declarative classes diverging like that, but for the sake of expansion, I'd be cautious of blessing into a subclass.
I forgot to say, an alternative might be to test the string to decide which kind of subclass you need, then create an instance of that subclass by calling its own constructor, but in each of the constructors for the subclasses, have a call to the parent's constructor (like super in Java). This is what I did, and the PerlMonks very kindly showed my how to do it. | [reply] [d/l] [select] |
I'd say "As long as the classes support the same set of methods, go ahead."
Jenda
| [reply] |
I use the "Factory method" in C++, and used it a few times last week.
It seems that you're planning to directly bless the instance into different child classes.
It would be clean, and quite acceptible, to call the proper constructor for each derived class instead. That is, the factory function figures out which derived class to use, then calls 'new' on it. The factory method never directly creates or blesses anything. | [reply] [d/l] |
Thank you. I did as you and Basilides suggested. The factory method determines the class name, then calls the subclass constructor like this:
return $class_name->new(%args)
This is cleaner than blessing into the subclass in the "parent".
I also more or less incorporated Jenda's suggestion and made the subclasses all support the same methods (except for some accessors that differ across subclasses). | [reply] [d/l] |