我有两个枚举,它们之间有映射。
Enum1:
public enum HttpMethodName
{
GET, POST, PUT, DELETE;
}
Enum2:
public enum ProtocolOperation {
CREATE(1), RETRIEVE(2), UPDATE(3), DELETE(4), NOTIFY(5);
private BigInteger operationId;
public BigInteger getOperationId() {
return operationId;
}
private ProtocolOperation(int operationId) {
this.operationId = BigInteger.valueOf(operationId);
}
}
枚举值的映射为:
Create--> POST
Retrieve--> GET
Update--> PUT
Delete--> DELETE
Notify---> POST
提供映射的一种方式将是通过binder名称创建第三个枚举:
Enum 3:
CREATE(1, HttpMethodName.POST),
RETRIEVE(2, HttpMethodName.GET),
UPDATE(3, HttpMethodName.PUT),
DELETE(4, HttpMethodName.DELETE),
NOTIFY(5, HttpMethodName.POST);
其他方法是仅保留两个枚举,并修改Enum2本身以进行映射。
哪种方法会更好?
我觉得在接近1时会有分离,虽然我觉得第三个枚举不是一个常量类型(这是枚举的主要目的),但它将是一个活页夹。
有一点需要注意的是,随着代码的增长,可能会出现更多绑定。对于另一种类型的协议,可能存在关于CREATE
等的第二种绑定。
答案 0 :(得分:1)
我会添加一个公共方法xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE ) {
if(xhttp.status == 200){
console.log(xhttp.responseText);
}
}
}
(将处理映射)到getHttpMethod()
,如下所示:
ProtocolOperation
例如
public enum ProtocolOperation {
CREATE(1){
@Override
public HttpMethodName getHttpMethodName(){
return HttpMethodName.POST;
}
},
RETRIEVE(2){
@Override
public HttpMethodName getHttpMethodName(){
return HttpMethodName.GET;
}
},
UPDATE(3){
@Override
public HttpMethodName getHttpMethodName(){
return HttpMethodName.PUT;
}
},
DELETE(4){
@Override
public HttpMethodName getHttpMethodName(){
return HttpMethodName.DELETE;
}
},
NOTIFY(5){
@Override
public HttpMethodName getHttpMethodName(){
return HttpMethodName.POST;
}
};
private BigInteger operationId;
public BigInteger getOperationId() {
return operationId;
}
private ProtocolOperation(int operationId) {
this.operationId = BigInteger.valueOf(operationId);
}
abstract public HttpMethodName getHttpMethodName();
}