Magento2 factory pattern advantages over Magento 1
I may be wrong here, but this is an advantage I found.
The auto generated factories are some how similar with the magic getters or setters.
Let's say you want something to happen when an instance of a specific entity (let's call it BlogPost) is created. Let's say you want to set a default value to a field.
The example may not be the best but hear me out.
If you use an abstract factory, you will have to modify it so that when you receive the instanceName as parameter 'BlogPost' you call setDate
after instantiating.
If you use autogenerated factory, you can later create that factory, call the setter
in your code, remove the generated factory and it will work.
Similar to what you do with the magic setter. You implement the method and it's called everywhere.
One thing to remember is we auto-generate factory classes ONLY IF YOU DON'T DEFINE ONE YOURSELF. That means, if you need to do some special magic in the factory, you can do so. (E.g. if you want to log every creation of an instance for some reason, write the factory yourself and we won't auto-generate it.) If we used a single abstract factory class for everything, this would not work.
It can also help a bit with debugging - you get to see the real class, can set breakpoints, see more meaningful stack-traces etc.