A
Alf P. Steinbach
* Noah Roberts:
You never do.
However, as I mentioned and you snipped, there are valid reasons for using
factory functions, in other contexts.
You mention one of those below (plus one that might be valid, depending on the
concrete details, plus some invalid ones ;-) ).
That's more complicated to do from scratch with the non-factory approach, yes.
It's one case where a class specific set of factory functions might be a good
solution.
At least if you're lacking some infrastructure.
You don't. That's totally evil. You're *changing the behavior* without changing
the type, leaving client code stranded with mysterious behavior change.
They do.
You don't.
That's one of the cases where a set of factory functions absolutely isn't the
answer.
There is no advantage.
But there is far more code.
It's like delving into assembly code in order to do multiplication, when C++
offers a perfectly good multiplication operator.
It's just completely brain-dead.
Yes, there are some valid uses for factory functions. And the above might be
one, depending on the concrete details. Ensuring dynamic allocation is however
not a valid reason; for that it's just plain silly to code up a set of factory
functions when there is a very simple almost no-cost way to achieve it.
Cheers & hth.,
- Alf
Well, for one, what if you want to change what's returned from the
constructor?
You never do.
However, as I mentioned and you snipped, there are valid reasons for using
factory functions, in other contexts.
You mention one of those below (plus one that might be valid, depending on the
concrete details, plus some invalid ones ;-) ).
There's the one case where you want to ensure that the
object is always created with a shared_ptr (as in you're using
shared_from_this),
That's more complicated to do from scratch with the non-factory approach, yes.
It's one case where a class specific set of factory functions might be a good
solution.
At least if you're lacking some infrastructure.
and there's the other case when you don't care but
sometime down the road someone decides that a subclass should be
returned rather than the one you're constructing.
You don't. That's totally evil. You're *changing the behavior* without changing
the type, leaving client code stranded with mysterious behavior change.
Clients don't
actually need to know this
They do.
but if you didn't wrap the constructor in a
factory method or object then they are stuck changing...every single one
that created the object.
You don't.
The guys at Net Objectives actually recommend doing it all the time but
they're working with languages like Java. You just can't in C++ and
even if you did (because you could I suppose return the base as a value)
it would just lose any extra information gained by subclassing. But if
you're positive that nobody should be creating some class on the stack
then it can be a very positive step to simply wrap the constructor(s) in
factory functions.
That's one of the cases where a set of factory functions absolutely isn't the
answer.
There is no advantage.
But there is far more code.
It's like delving into assembly code in order to do multiplication, when C++
offers a perfectly good multiplication operator.
It's just completely brain-dead.
They don't have to be the AbstractFactory object,
but a basic FactoryMethod can provide this service.
The abstract factory works well when you're creating a conglomeration of
objects based upon the parameters passed in. It can be the one, sole
object in your design that has to know about the whole tree of classes,
pick the best one(s), and build what clients need. That's really the
only thing I ever use it for. If I find myself implementing an NxM set
of methods in this factory, as you indicate, I ask myself if it's really
the best thing for the job.
Yes, there are some valid uses for factory functions. And the above might be
one, depending on the concrete details. Ensuring dynamic allocation is however
not a valid reason; for that it's just plain silly to code up a set of factory
functions when there is a very simple almost no-cost way to achieve it.
Cheers & hth.,
- Alf