Responding to El Aceitoso...
I'd like to have a function factory that returns objects of a class hierarchy.
What's the best way to deal with the fact that different subclasses will have
different constructor arguments?
How do you keep the factory interface as clean as possible? O
If the subclasses have different arguments (as opposed to argument
values), then Factory is not going to work well because it depends upon
inclusion polymorphism, which requires a common (abstract) interface.
To deal with your sort of situation the answer lies in parametric
polymorphism. Basically, the <concrete> Factory itself goes and gets
the data that it needs by navigating back to the Context object or to
some sort of specification object. It then uses that data to do the
right thing.
The former is most useful when the Context has all the necessary data,
but only some of it is needed for each constructor. Use an Abstract
Factory pattern and instantiate the relationship between Context and the
relevant concrete Factory. The concrete Factory navigates back to the
Context to get what it needs and then invokes the right constructor.
(The same thing can be done if the necessary data resides in other
objects so long as they can be unambiguously reached via relationships
through the Context object.)
The specification object is useful when the decision data is not readily
available as Context attributes. For example, the subclass to create
may be dictated by an XML string from an external configuration file.
Typically the external configuration data will be placed in a
specification object and a relationship between it and the Factory will
be instantiated along with that to the Context.
A common trick in this situation is to have a flock of private
construction methods in the Factory and a jump table to them that is
indexed by some identity attribute in the specification object. The
private method then parses the rest of the specification data and
invokes the right constructor.
*************
There is nothing wrong with me that could
not be cured by a capful of Drano.
H. S. Lahman
(e-mail address removed)
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindersol.com
(888)-OOA-PATH