views:

38

answers:

3

If I have a factory that creates an object that needs an instance of another object should i use another factory responsible for this second's object creation or should the original factory handle this?

+1  A: 

As is the answer to most design question, it depends. If virtually any instance of the other object can be used to initialize the first object then probably yes. That will make them more independent, but your code will grow (usual tradeoff). On the other hand, if specific objects require specific other objects then this should be one factory (or an abstract factory)

Armen Tsirunyan
+1  A: 

It depends entirely on the nature of the second object. Does it "belong" to this factory? If so, then this factory should handle it. If not, maybe another factory (or something else entirely) should do the work.

The trick to this kind of question is knowing when to ignore the rules of software engineering.

Cameron Skinner
+1  A: 

As the two other answers stated, it depends on the level of abstraction you need. Take testability and extensibility into consideration. If the second object is intended to be created via a Factory Method, then yes. You weave both patterns to work together. Just treat it like a black box how you initially wanted.

Add more detail if you'd like a more detailed answer from me.

Mike