/**
* 在工厂模式里面利用反射机制动态调用对象方法
*
* @author zhang.brian@nstechs.com
*
*/
public class PersonFactory {
private static final int PERSON_TYPE_CHINESE = 1;
private static final int PERSON_TYPE_AMERICAN = 2;
private static final int PERSON_TYPE_FRANCH = 3;
private static final int PERSON_TYPE_JAPANESE = 4;
private static Map<Integer, Object> persons = new HashMap<Integer, Object>();
static {
persons.put(new Integer(PERSON_TYPE_CHINESE), Chinese.class);
persons.put(new Integer(PERSON_TYPE_AMERICAN), American.class);
persons.put(new Integer(PERSON_TYPE_FRANCH), Franch.class);
persons.put(new Integer(PERSON_TYPE_JAPANESE), Japanese.class);
}
/**
* 在工厂模式里面利用反射机制动态调用对象方法
*
* @param type
* 对象类型
* @return 被实例化后的person对象
*/
public static Person getPerson(int type) {
if (obj instanceof Class<?>) {
s = (Class<?>) obj;
}
try {
Method m = s.getMethod("say", new Class[] { String.class });
m.invoke(s.newInstance(), new Object[] { new String("您好") });
return (Person) s.newInstance();
} catch (Exception e) {
System.out.println(new RuntimeException("class is not find"));
return null;
}
}
public static final void main(String agrs[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int type = Integer.parseInt(br.readLine());
Person person = PersonFactory.getPerson(type);
person.say("hello");
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
* 在工厂模式里面利用反射机制动态调用对象方法
*
* @author zhang.brian@nstechs.com
*
*/
public class PersonFactory {
private static final int PERSON_TYPE_CHINESE = 1;
private static final int PERSON_TYPE_AMERICAN = 2;
private static final int PERSON_TYPE_FRANCH = 3;
private static final int PERSON_TYPE_JAPANESE = 4;
private static Map<Integer, Object> persons = new HashMap<Integer, Object>();
static {
persons.put(new Integer(PERSON_TYPE_CHINESE), Chinese.class);
persons.put(new Integer(PERSON_TYPE_AMERICAN), American.class);
persons.put(new Integer(PERSON_TYPE_FRANCH), Franch.class);
persons.put(new Integer(PERSON_TYPE_JAPANESE), Japanese.class);
}
/**
* 在工厂模式里面利用反射机制动态调用对象方法
*
* @param type
* 对象类型
* @return 被实例化后的person对象
*/
public static Person getPerson(int type) {
Object obj = persons.get(type);
Class<?> s = null;if (obj instanceof Class<?>) {
s = (Class<?>) obj;
}
try {
Method m = s.getMethod("say", new Class[] { String.class });
m.invoke(s.newInstance(), new Object[] { new String("您好") });
return (Person) s.newInstance();
} catch (Exception e) {
System.out.println(new RuntimeException("class is not find"));
return null;
}
}
public static final void main(String agrs[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int type = Integer.parseInt(br.readLine());
Person person = PersonFactory.getPerson(type);
person.say("hello");
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
输入:1
Chinese say:您好
Chinese say:hello