I have an object guiObject that comes from GUI. Based on its data fields I need to instantiate domainObject of proper class. It can either be DomainClassA or DomainClassB.
DomainClassA has one Integer contructor parameter intParamA (comes from guiObject.fieldA).
DomainClassB has one Integer contructor parameter intParamB (comes from guiObject.fieldB).
To solve this problem I made AbstractFactory that takes required fields from guiObject, instantiates either DomainClassAFactory or DomainClassBFactory with proper fields from GuiClass and retuns it. In turn, either of those factories create() properly instantiated domainObject.
But now, depending on guiObject.fieldC I need to alter intParamA and intParamB (i.e. decrease by 1) before instantiating domainObject. To achieve that I had to create separate factories for each different type of “parameter altering” for each DomainClass and then create separate abstract factories that produce correct factories. That sounds ugly and that looks ugly.
What should be the correct design?
Unless you need all those layers, have a factory that interrogates the
guiObjectand returns the right type.Over-analyzing leads to way too much stuff that most apps simply don’t need. Add additional layers of abstraction only if they’re strictly necessary. They’re usually not.