public interface Behavior {
void doAction();
}
public class BehaviorA implements Behavior {
public void doAction() {
}
}
public class BehaviorB implements Behavior {
public void doAction() {
}
}
behavior.class=com.example.BehaviorA
public class BehaviorFactory {
public static Behavior createBehavior() {
Behavior behavior = null;
try {
Properties properties = new Properties();
properties.load(BehaviorFactory.class.getResourceAsStream("config.properties"));
String className = properties.getProperty("behavior.class");
Class<?> behaviorClass = Class.forName(className);
behavior = (Behavior) behaviorClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return behavior;
}
}
public class Main {
public static void main(String[] args) {
Behavior behavior = BehaviorFactory.createBehavior();
behavior.doAction();
}
}