package simpleFactory;
public interface People {
void say();
}
package simpleFactory;
public class Man implements People{
public void say() {
System.out.println("男人");
}
}
package simpleFactory;
public class Woman implements People{
public void say() {
System.out.println("女人");
}
}
package simpleFactory;
public class SimpleFactory {
public static People create(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
Class class1 = Class.forName(className);
return (People) class1.newInstance();
}
}
package simpleFactory;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
People man = SimpleFactory.create("simpleFactory.Man");
People woman = SimpleFactory.create("simpleFactory.Woman");
man.say();
woman.say();
}
}