Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Java 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(); }




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: