简易框架

//方法一:多继承
/*
//class mainop:public CSocketProtocol, public CEncDesProtocol{

}*/

//方法二:组合,在类里面组合对象
class mainop{
public:
    mainop(){
        this->sp = NULL;
        this->ed = NULL;
    }
    mainop(CSocketProtocol *sp, CEncDesProtocol *ed){
        this->sp = sp;
        this->ed = ed;
    }
    //注入接口
    setEd(CSocketProtocol *ed)
    {
        this->ed = ed;
    }
    //注入接口
    setSp(CSocketProtocol *sp)
    {
        this->sp = sp;
    }
    
    //业务框架
    int SckSendAndRec_EncDec(unsign char *in, int inlen, unsign char *out, int outlen)
    {
        int ret = 0;
        unsign char data[4096] = {0};
        int datalen;
        
        int ret = sp->cltSocketInit();
        if (ret != 0){
            return ret;
        }
        
        ret = ed->Encdata(in, inlen, data, &datalen);
        if (ret != 0){
            goto end;
        }
        
        ret = sp->cltSocketSend(data, datalen);
        if (ret != 0){
            goto end;
        }
        
        ret = sp->cltSocketRec(data, &datalen);
        if (ret != 0){
            goto end;
        }
        
        ret = ed->Desdata(data, datalen, out, outlen);
        if (ret != 0){
            goto end;
        }
        
    end:
        sp->cltSocketdestory();
    }
    
    CSocketProtocol *sp;
    CEncDesProtocol *ed;
    

}
int main(){
    int ret = 0;
    unsign char in[4096] = {0};
    unsign char out[4096] = {0};
    int inlen = 0;
    out outlen = 0;
    
    strcpy(in, "11111111111");
    int inlen = strlen(in);
    
    mainop *mymainop = new mainop();
    CSocketProtocol *sp = new CSckFactory1();
    CEncDesProtocol *ed = new CHwEncDes();
    mainop->setsp(sp);
    mainop->setEd(ed);
    
    ret = mymainop->SckSendAndRec_EncDec(sp, ed, in, inlen, out, &outlen);
    if (ret != 0) {
        
        return ret;
    }
    
    delete sp;
    delete ed;
    delete mymainop;
    
    return 0;
}
/*业务框架,多态的平台
int SckSendAndRec_EncDec(CSocketProtocol *sp, CEncDesProtocol *ed, unsign char *in, int inlen, unsign char *out, int outlen)
{
    int ret = 0;
    unsign char data[4096] = {0};
    int datalen;
    
    int ret = sp->cltSocketInit();
    if (ret != 0){
        return ret;
    }
    
    ret = ed->Encdata(in, inlen, data, &datalen);
    if (ret != 0){
        goto end;
    }
    
    ret = sp->cltSocketSend(data, datalen);
    if (ret != 0){
        goto end;
    }
    
    ret = sp->cltSocketRec(data, &datalen);
    if (ret != 0){
        goto end;
    }
    
    ret = ed->Desdata(data, datalen, out, outlen);
    if (ret != 0){
        goto end;
    }
    
end:
    sp->cltSocketdestory();
}
int main(){
    int ret = 0;
    unsign char in[4096] = {0};
    unsign char out[4096] = {0};
    int inlen = 0;
    out outlen = 0;
    
    strcpy(in, "11111111111");
    int inlen = strlen(in);
    
    CSocketProtocol *sp = new CSocketProtocol();
    CEncDesProtocol *ed = new CEncDesProtocol();
    
    ret = SckSendAndRec_EncDec(sp, ed, in, inlen, out, &outlen);
    if (ret != 0) {
        
        return ret;
    }
    
    delete sp;
    delete ed;
    
    return 0;
}*/
原文地址:https://www.cnblogs.com/jly594761082/p/10550365.html