Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://stackoverflow.com/questions/31768610/reflection-in-factory-design-patterns-in-java-or-c-sharp
- class PersonFactory {
- // reflection, by full qualified class name
- public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {
- Class<?> personClass = Class.forName(personType);
- return getPersonWithClass(personClass);
- }
- // reflection, by passing class object
- public static Person getPersonWithClass(Class personClass) throws Exception {
- return (Person) personClass.newInstance();
- }
- // no reflection, the ordinary way
- public static Person getPersonWithName(String personType) {
- if (personType.equalsIgnoreCase("STUDENT")) {
- return new Student();
- } else if (personType.equalsIgnoreCase("TEACHER")) {
- return new Teacher();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement