用枚举实现工厂模式


public enum AgentType {
HTTP("http") {
public Agent newInstance() {
return new HttpAgent();
}
},
WEBSERVICE("webSerivce") {
public Agent newInstance() {
return new WSAgent();
}
};

public abstract Agent newInstance();

private String op;

private AgentType(String op) {
this.op = op;
}

public String getOp() {
return op;
}

public static Agent getAgent(String op) {
for (AgentType type : AgentType.values()) {
if (type.getOp() == op) {
return type.newInstance();
}
}
return null;
}
}

你可能感兴趣的:(设计模式)