如附件图片。这个设计模式很通用,对任何业务中遇到的有request和response的类似command的操作都可以应用这种模式,具体的业务方法可以实现在CMDBuilder类中的buildRequest()和buildResponse()方法中,以下的CMD是一个通用的command类,任何业务实现类如果要写 buildRequest()和buildResponse()方法的话都可以继承这个类, CMDAdapter的execCMD需要用户根据不同业务来有自己具体实现,或借助第三方,譬如利用advent TL1 API和SNMP4j建立TL1和snmp的通道代码,做的好的话可以把这些通道代码分离出来,execCMD中只需要利用这个通道发送command就可以了。
public interface CMDAdapter {
CMDResponse execCMD(CMDRequest cmd) throws Exception;
}
/**
* @author will
* <p>
* the command request interface
* <p>
* CMD client: any command(SNMP, TL1, RPC, etc.) service consumer
* <p>
* CMD server: any command(SNMP, TL1, RPC, etc.) service provider
*/
public abstract class CMDBuilder {
/**
* build the request for CMD server from the request data from CMD client
*/
protected abstract void buildRequest() throws Exception;
/**
* build the response for CMD client from the response data from CMD server
*/
protected abstract void buildResponse() throws Exception;
}
/**
* @author will
* <p>
* the command request interface
* <p>
* CMD client: any command(SNMP, TL1, RPC, etc.) service consumer
* <p>
* CMD server: any command(SNMP, TL1, RPC, etc.) service provider
*/
public interface CMDRequest {
/**
* @param requestData
*
* set the request data from CMD client
*/
void setRequestData(Object requestData) throws Exception;
/**
* @return the request data from CMD client
*/
public Object getRequestData();
/**
* @param request
*
* set the request object for CMD server
*/
void setRequest(Object request);
/**
* @return the request for CMD server
*/
Object getRequest() throws Exception;
/**
* @param request
*
* set who execute the request
*/
void setRequestor(Object requestor);
/**
* @return who execute the request
*/
Object getRequestor();
}
/**
* @author will
* <p>
* the command request interface
* <p>
* CMD client: any command(SNMP, TL1, RPC, etc.) service consumer
* <p>
* CMD server: any command(SNMP, TL1, RPC, etc.) service provider
*/
public interface CMDResponse {
/**
* @param responseData
*
* set the response data from CMD server
*/
void setResponseData(Object responseData) throws Exception;
/**
* @return the response data from CMD server
*/
public Object getResponseData();
/**
* @param responseCode
*
* set the response code to explain the result of the command execution on
* CMD server
*/
void setResponseCode(int responseCode);
/**
* @return the response code to explain the result of the command execution
* on CMD server
*/
int getResponseCode();
/**
* @param response
*
* the response for CMD client
*/
void setResponse(Object response);
/**
* @return the response for CMD client
*/
Object getResponse() throws Exception;
/**
* @param ex
*
* the exception for CMD client
*/
void setException(Exception ex);
/**
* @return the exception for CMD client
*/
Exception getException();
}
/**
* @author will
*
* CMD object for all the adapters(SNMP, TL1, RPC, etc.)
*
*/
public abstract class CMD extends CMDBuilder implements Serializable,
CMDRequest, CMDResponse {
/**
* the request data from CMD client
*/
protected Object requestData;
/**
* the request object for CMD server, It can be a RPC PDU(byte[]), TL1
* command(String) or SNMP command(SNMP get).
*/
protected Object request;
/**
* who execute the request
*/
protected Object requestor;
/**
* the response data from CMD server
*/
protected transient Object responseData;
/**
* the response code to explain the result of command execution on CMD
* server
*/
protected int responseCode;
/**
* the response object for CMD client
*/
protected Object response;
/**
* the exception for CMD client
*/
protected Exception exception;
public Object getRequestData() {
return this.requestData;
}
public void setRequestData(Object requestData) throws Exception {
this.requestData = requestData;
buildRequest();
}
public void clearRequestData() {
this.requestData = null;
}
public Object getRequest() throws Exception {
if (this.request == null)
buildRequest();
return this.request;
}
public void setRequest(Object request) {
this.request = request;
}
public Object getRequestor() {
return this.requestor;
}
public void setRequestor(Object requestor) {
this.requestor = requestor;
}
public void clearRequest() {
this.setRequest(null);
}
public Object getResponseData() {
return this.responseData;
}
public void setResponseData(Object responseData) throws Exception {
this.responseData = responseData;
buildResponse();
}
public void clearResponseData() {
this.responseData = null;
}
public int getResponseCode() {
return this.responseCode;
}
public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}
public Object getResponse() throws Exception {
if (this.response == null)
buildResponse();
return this.response;
}
public void setResponse(Object response) {
this.response = response;
}
public void clearResponse() {
this.setResponse(null);
}
public Exception getException() {
return this.exception;
}
public void setException(Exception ex) {
this.exception = ex;
}
}