▲ | munksbeer 18 hours ago | |
Factories are a pattern that is not unique to Java. They're used prolifically in C++ at least, and I'm sure in other languages. The factory pattern is tightly coupled with polymorphism. If you're not using polymorphism, then you may not typically need factories. But if you are, then at some point you're going to have some conditional logic about what concrete type of class you're going to create of a particular interface. Modern Java is lending itself more towards data oriented programming, and coders are trending in that direction. This may or may not last, like every trend. But even then, at the moment this tends to still involve using sealed interfaces and records, so you still have some form of instance creation and polymorphism. How do you avoid factories? Doesn't you code end up littered with worse code? | ||
▲ | nunobrito 8 hours ago | parent [-] | |
Thanks for the explanation, very detailed. > How do you avoid factories? Doesn't you code end up littered with worse code? Easier for me to use "public abstract class" and then leave open methods inside like "protected abstract doAction();" that are forcefully implemented on each variation of that class. There is still just one base implementation, I guess the main difference is using strong typed classes that keep the specific code inside specific classes. With factories one tends to end up with a very large source code file that details with too many specific different topics, was making maintenance and testing harder afterwards. Or maybe I just never really understood properly the value of factories. |