command adapter 设计

本文介绍了一种通用的Command模式设计,适用于多种业务场景中的请求与响应处理,如SNMP、TL1等。通过CMDBuilder抽象类及CMD接口定义了请求与响应的基本结构,并提供了扩展点以适应不同业务需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 sequence diagram如附件图片。这个设计模式很通用,对任何业务中遇到的有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;
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值