Should a factory use a different factory if an object needs another object to create?

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

+3
source share
3 answers

Like the answer to most design questions, it depends. If almost any instance of another object can be used to initialize the first object, then probably yes. This will make them more independent, but your code will grow (regular compromise). On the other hand, if specific objects require specific other objects, then this should be a single factory (or abstract factory)

+2
source

It completely depends on the nature of the second object. Does it "belong" to this factory? If so, then this factory should process it. If not, perhaps another factory (or something else) should do the job.

The trick with this question is to know when to ignore software development rules.

+1
source

As the other two answers say, it depends on the level of abstraction that you need. Consider validation and extensibility. If the second object must be created using the Factory method, then yes. You combine both templates for collaboration. Just treat it like a black box, as you originally wanted.

Add more details if you want a more detailed answer from me.

+1
source

Source: https://habr.com/ru/post/1771539/


All Articles