Let's keep it simple...
class Client
abstract class AbstractBusinessObject
class BusinessObject
class BusinessObjectFactory
Okay, so your
Client
needs to perform some operations onAbstractBusinessObject
. Obviously somewhere oneBusinessObject
instance needs to be created. Why not in the client? What's the harm?Let's assume you convinced me to create new
BusinessObjects
in myBusinessObjectFactory
. Great. Now the factory has to do the heavy lifting. Let's assume my factory has aCreate(DataRow row)
method because myBusinessObject
needs the DataRow to extract useful information from it.
MyBusinessObject's
properties are{get; private set;}
. Therefore I cannot assign any property values in my factory. Probably I shouldn't anyway. This however means I have to pass along theDataRow
entirely. So basically the factory just passes along my parameter. What's the use of the factory here?Ok so I might have different implementation of
BusinessObject
- each of them having different constructors. I guess it makes sense for the factory to know how to construct any specificBusinessObject
then. But then again I need a specific factory to get a specificBusinessObject
. At least all examples I saw suggest just that.ConcreteFactoryB
createsConcreteProductB
(ConcreteFactoryA
createsConcreteProductA
) How does the client know how to construct the factory? Or even which one to create?Ah the factory is injected... composition root. Right. But I could have injected the BusinessObject directly just as well.
The example over at DoFactory made it a little bit more clear I think. Is it all about making sure the
Lion
doesn't eatBisons
? And theWolf
doesn't feed onWildebeests
? If that's all the factory tries to assure I don't need one if I just want to create a single object, right?
So does a factory like this one here make any sense? Does a factory ever make sense if I only have one Create()
method which only makes one type of classes?
Really, I read a lot about it. Every example looks like the other. I'm missing the point :( Can anyone provide a crystal clear real world example?
https://stackoverflow.com/a/2280289/1407618
Been there, done that...