jeudi 5 février 2015

Factory pattern and my incapacity. Help me see the light

Let's keep it simple...



class Client
abstract class AbstractBusinessObject
class BusinessObject
class BusinessObjectFactory




  1. Okay, so your Client needs to perform some operations on AbstractBusinessObject . Obviously somewhere one BusinessObject instance needs to be created. Why not in the client? What's the harm?




  2. Let's assume you convinced me to create new BusinessObjects in my BusinessObjectFactory. Great. Now the factory has to do the heavy lifting. Let's assume my factory has a Create(DataRow row) method because my BusinessObject needs the DataRow to extract useful information from it.

    My BusinessObject'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 the DataRow entirely. So basically the factory just passes along my parameter. What's the use of the factory here?




  3. 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 specific BusinessObject then. But then again I need a specific factory to get a specific BusinessObject. At least all examples I saw suggest just that. ConcreteFactoryB creates ConcreteProductB (ConcreteFactoryA creates ConcreteProductA) How does the client know how to construct the factory? Or even which one to create?




  4. Ah the factory is injected... composition root. Right. But I could have injected the BusinessObject directly just as well.




  5. The example over at DoFactory made it a little bit more clear I think. Is it all about making sure the Lion doesn't eat Bisons? And the Wolf doesn't feed on Wildebeests? 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?


http://ift.tt/1DK5IbH

Been there, done that...


Aucun commentaire:

Enregistrer un commentaire