r/AskProgramming • u/DarkSlayer_SM • Oct 06 '26
Why can't we use the Factory Method pattern in place of Abstract Factory?
I was discussing design patterns with a senior engineer and got asked a question that I couldn't answer convincingly:
"Why do we need Abstract Factory? Why can't we achieve the same thing using Factory Method?"
My current understanding is:
- Factory Method delegates the creation of a product to subclasses, allowing the subclass to decide which concrete product gets created.
- Abstract Factory provides an interface for creating a family of related products, while keeping the products consistent with each other.
After thinking about it, I realized that with a regular factory/Factory Method approach, the responsibility of selecting the product family has to be handled somewhere else.
With an Abstract Factory, that family selection is encapsulated in the factory object itself.
For example, imagine we have:
UI
├── Button
└── Checkbox
Windows
├── WindowsButton
└── WindowsCheckbox
Mac
├── MacButton
└── MacCheckboxAn Abstract Factory could look conceptually like:
UIFactory
createButton()
createCheckbox()
WindowsFactory
createButton() -> WindowsButton
createCheckbox() -> WindowsCheckbox
MacFactory
createButton() -> MacButton
createCheckbox() -> MacCheckboxBut couldn't I instead have separate Factory Methods for Button and Checkbox and somehow select the appropriate implementation for each?
If so, what fundamental capability does Abstract Factory provide that Factory Method doesn't?
Is the real distinction primarily about where the creation/family-selection responsibility lives, rather than simply:
Factory Method = one product
Abstract Factory = family of productsAnd are there concrete situations where using multiple Factory Methods would become problematic compared with using an Abstract Factory?
I'd especially appreciate an explanation from people who have used these patterns in real systems rather than just the textbook definitions.
24
u/Saki-Sun Oct 06 '26
At some point you need to just solve problems, stop looking for solutions...
Sure, GOF is great. But the whole idea is to provide a common language for solving well known problems. Not to collect them like Pokemon.
A factory pattern is enough, people grok it.
4
u/samsly135 Oct 06 '26
Because sometimes you want to have a group of related objects created. Dark theme would issue dark ui elements, light theme light elements. Say you have 10 gui elements. With factory method you would have to maintain all 10 factory methods and relate them yourself.
3
u/danielt1263 Oct 06 '26
If you were using Factory Methods, how would you ensure that the program doesn't put a MacButton in a LinuxWindow?
3
u/Underhill42 Oct 06 '26
Yes. And one of the easiest ways to do that is with an Abstract Factory.
Because then you don't have to worry (in code) about what kind of object it is when you're creating it. You've got a Abstract Factory box that was previously filled with the right kind of factory, and you just tell it "make me one of whatever it is you make".
One example where that might be useful is if you want to create an object based on an identifier. If I have 10 different kinds of objects that I can create, I could write a big long switch statement to pick between them. Or I could have one function that says "They selected item 7, tell the factory in box 7 to create an object".
Then the only thing you need to be able to add a new object is add another factory to the list of factories, and then anywhere in the code that needs to create an object will automatically know how to create an object of "type 11"
2
u/marrsd Oct 06 '26 edited Oct 07 '26
I got tired of this sort of masturbation years ago, and arguments like this are why. My advice to both you and your colleague is to dedicate your brain power to solving some real problems, preferably in a paradigm that doesn't require these sorts of distractions.
1
u/w0ut Oct 06 '26
https://www.reddit.com/r/learnprogramming/comments/1bikubq/difference_between_factory_method_and_abstract/
1
u/Ollidav Oct 06 '26
Una respuesta rápida podría ser que para un Factory necesitas implementar una interfaz que limite los métodos que se pueden heredar. Es básicamente lo que hace un patrón Factory.
1
u/Jonny0Than Oct 06 '26
A factory method is a single Create function added to an existing class hierarchy that is not directly related to the thing it’s creating. For example, different unit types in a RTS game might have a factory method for creating their command UI. Each unit type can do different things. The unit class hierarchy is not a factory, but you gain some organization and encapsulation by moving the UI creation into the unit class.
An abstract factory is a whole class hierarchy that is only concerned with creating objects from a related family. Typically you’d create one instance of one of the concrete factory classes for the duration of the program (singleton). This is also very useful if you anticipate needing new factory types (you made windows and Mac UI factories, but might add Linux later) or mock objects for testing.
And to wrap it up, perhaps that CreateCommandUI in the unit class is invoking methods on a singleton UIWidgetFactory!
1
u/sporbywg Oct 06 '26
You can; and later, a more experienced programmer will have to fix it.
1
u/mredding Oct 07 '26
THAT'S IT, THAT'S THE CRUX.
How do you select the appropriate implementation for each? "Somehow" isn't good enough. No more hand waving - you have to actually decide on an implementation.
"Somehow" is your abstract factory. This is what it does. Is it a Windows, Linux, or Mac button? At this higher level, I don't give a shit.
The decision has to be made at some point, somewhere, somewhen. Since we're talking about different platforms, you might make this decision when you select for different build targets. If you're making a universal binary, then the decision making will have to happen at runtime. But that decision only has to be made once during execution, during startup/setup, and the consequence of that can be expressed in terms of an abstract factory - it's set up with the family of factories you're going to need. Your code is built against this one uniform interface so you can GET the factories you need.
0
u/BoBoBearDev Oct 07 '26
Factory hurts my brain, so I just use microservices with tightly coupled tiny service code. And frontend, it is ReactJs, there is no platform. Or Avalonia, there is no platform.
Why factory hurts my brain? Because the runtime doesn't match the source code. It changes during configuration. So, I can't do anything meaningful until I pieces together all the data to find out which component is used at runtime. It is too much work to trace.