public interface Behavior { void execute(); } public class BehaviorFactory { public static Behavior createBehavior(String behaviorType) { if (behaviorType.equals("A")) { return new BehaviorA(); } else if (behaviorType.equals("B")) { return new BehaviorB(); } else { return null; } } } plaintext behaviorType=A public class Main { public static void main(String[] args) { String behaviorType = readBehaviorTypeFromConfig(); Behavior behavior = BehaviorFactory.createBehavior(behaviorType); behavior.execute(); } private static String readBehaviorTypeFromConfig() { return "A"; } }


上一篇:
下一篇:
切换中文