dimanche 12 août 2018

Can a factory class in a Factory Design Pattern be dependent on some other independent factory class?

Scenario:

EntityA -> Independent

EntityB -> Independent

EntityC -> Composed of EntityA, EntityB and some other data

Object creation of EntityC requires some data from EntityA and EntityB and I am confused on how to structure the factory class for EntityC

Approach 1:

Passing the data required to create EntityA and EntityB as argument to the FactoryC.create() and import FactoryA and FactoryB to initialize objects inside FactoryC.create()

Here, I can keep the logic for object creation completely isolated from my application (which is the main purpose of using Factories), but FactoryC would be dependent on FactoryA and FactoryB

Approach 2:

Initializing objects of EntityA and EntityB in my application (outside the scope of factories) and passing these objects to FactoryC.create()

Here, FactoryC would be independent of FactoryA and FactoryB but the downside is that I'll have to put the logic for object creation in my application (outside factories)

Programatically both the approaches will give the same result but I want to know which one will be better in terms of structuring my code

Aucun commentaire:

Enregistrer un commentaire