Reputation: 1671
I have concrete factory classes which implement MyFactory
interface. I want to get concrete factory objects based on parameter. How to do this?
One way is to create factory of factories[FoF], and FoF would be parameterized factory.
Is there any more elegant way to do this?
This is purely intellectual exercise, I am not facing any problem in my project.
Upvotes: 3
Views: 1190
Reputation: 18034
If you are able to merge the two layers of factories, that would of course be the preferable solution. This would be possible if the have the same lifecycle.
If they don't then creating a factory of factories is fine. There's no problem with the pattern itself, the pattern can be nested. You could even create a factory of factories of factories (please don't) and it would work.
However, you should find a suitable name for the factory of factories, so that the intent is clear. Factory of factories doesn't sound like a good name to me. Choosing a name that communicates the behavior within your problem domain is better than a purely pattern-oriented name.
Edit: Someone suggested that you take a look at the abstract factory pattern in the comments to the question. While it is certainly a useful pattern, and one that a good software engineer should know about, an abstract factory is not a factory of factories: The pattern abstracts a group of related factories, but is not about creating them. Still, learning about it will help.
Upvotes: 4