4 ms·
What about the much maligned factory pattern If classes are first-class variables (as in Python, and now even Java (I think)), this is replaced with a construc
by andreyf 17y ago
What about the much maligned factory pattern
If classes are first-class variables (as in Python, and now even Java (I think)), this is replaced with a constructor which takes a class as an argument.
- mhansen 17y agoThen you aren't gaining anything over simply calling 'new' on that class, negating the point of the factory pattern, right?
- orangecat 17y agoThe point of the factory pattern is that you don't always want a new instance, and you don't always want a specific type. "new Foo()" necessarily does both, while Foo.new() can potentially do neither.
- lucifer 17y agoJava has always had the ability to reference Class types, and, to instantiate instances of that type using a standard method: Object makeOne (Class fooClass) { return fooClass.newInstance(); } You are missing the point of a factory pattern, which is informed by the non-accidental pattern name: 'Factory'. [edit: using generics it is even type safe]: <T> T makeOne (Class<T> clazz) { return clazz.newInstance(); }