状态设计模式-实例(C语言的代码用JAVA状态设计模式优化实现)

    一段复杂的逻辑(处理IC卡的CVM)的C代码,函数太臃肿(158行),不容易理解维护扩展,混在一起,改用状态设计模式实现。

1.C语言代码的逻辑:

根据EMV_StartCVM的执行结果判断是脱机,还是联机,还是要获取下一个CVM的动作。如果EMV_CompleteCVM 返回值不等于EMV_NEXT_CVM 才退出循环。脱机里又有个循环。

int AppEmvHolderVerify(void)
{
    int nRet =0,nPwdRst=0;
    unsigned char ucCVMType=0,ucPINCnt=0;
    unsigned char aucPINData[30] = {0x00};
    unsigned char aucPINForamt[30] = {0x00};
    unsigned char aucTVR[5] = {0x00};
    unsigned char ucPINTryCount =0;
    int nTryFlg=0;
    unsigned int nTVRlen = 0;
    unsigned char ucBypassedOne=0;
    unsigned char ucPINCancelFlag = 0;
	EMV_TERMPARAM tTermParam;
		
	memset(&tTermParam, 0x00, sizeof(EMV_TERMPARAM));
	GetTermParam(&tTermParam);

    memset(aucPINData,0,sizeof(aucPINData));
    memset(aucPINForamt,0,sizeof(aucPINForamt));
    memset(aucTVR,0,sizeof(aucTVR));

    while(1)
    {
        ucPINCancelFlag = 0;
		nRet=EMV_StartCVM(ucBypassedOne, &ucCVMType,&ucPINCnt);
		if(nRet)
        {
        	if(nRet == EMV_QUIT_CVM)
            	return EMV_OK;
            else
            	return nRet;
        }
        if(ucCVMType == EMV_CVM_SIGNATURE || ucCVMType == EMV_CVM_FAIL_CVM || ucCVMType == EMV_CVM_NO_CVM)    
        {
        	nRet=EMV_CompleteCVM(0,NULL,&ucPINTryCount);
        }
        else if(ucCVMType == EMV_CVM_ONLINE_PIN)
        {
			if (ucBypassedOne == 1 && tTermParam.ucBypassAllFlg == 1)
            {
                nRet = EMV_NEXT_CVM;
                continue;
            }
            nPwdRst = AppGetHolderPwd(0,0,NULL);
            gl_ucPinInput = 1;
			nRet =EMV_CompleteCVM(nPwdRst,NULL,&ucPINTryCount);
            if (nRet == EMV_NEXT_CVM) 
            {
                if(nPwdRst == EMV_NO_PASSWORD)
                {
                    ucBypassedOne = 1;
                    continue;
                }
            }
			else if (nRet == EMV_USER_CLEAR)
			{
				return EMV_USER_CLEAR;
			}
        }
        else
        {			
            while(2)
            {
                if (ucBypassedOne == 1 && tTermParam.ucBypassAllFlg == 1) 
                {
                    nRet = EMV_NEXT_CVM;
                    break;
                }
                nPwdRst = AppGetHolderPwd(nTryFlg, ucPINCnt, aucPINData);
                gl_ucPinInput = 1;
                if(DEV_IccGetTxnIF() == DEV_CONTACT_TXNIF)
                {
                    if (gl_ucPciFlg == 1)
                    {
                        aucPINForamt[0] = 0x00;
                        aucPINForamt[1] = 0x00;
                    }
                    else
                    {
                        aucPINForamt[0] = 0x01;
                        aucPINForamt[1] = strlen((char*)aucPINData);
                    }
                    memcpy(&aucPINForamt[2],aucPINData,aucPINForamt[1]);
					            nRet=EMV_CompleteCVM(nPwdRst,aucPINForamt,&ucPINTryCount);
                }
                else
                {
                    aucPINForamt[0] = 0x01;
                    aucPINForamt[1] = (unsigned char)(strlen((char*)aucPINData));
                    memcpy(&aucPINForamt[2],aucPINData,aucPINForamt[1]);
			nRet=EMV_CompleteCVM(nPwdRst,aucPINForamt,&ucPINTryCount);
                }
                if(nRet)
                {
                    if(nRet == EMV_USER_CANCEL && ucPINCancelFlag == 0 && gl_ucCancelPinFlag == 1)
                    {

						//AppEMVVerifyPINFail();

                        if (nRet == 0)
                        {
                            ucPINCancelFlag = 1;
                            continue;
                        }
                        else
                        {
                            nRet = EMV_NEXT_CVM;
							nTVRlen = 0;
							memset(aucTVR,0,sizeof(aucTVR));
                            AppGetTLVData(0x95, aucTVR, &nTVRlen);
                            aucTVR[2] |= 0x08;
                            AppSetTLVData(0x95, aucTVR, nTVRlen);
                        }
                        
                    }
                    else if(nRet == EMV_NEXT_CVM)
                    {
						nTVRlen = 0;
						memset(aucTVR,0,sizeof(aucTVR));
                        AppGetTLVData(0x95, aucTVR, &nTVRlen);
						if((aucTVR[2] &0x08 ) == 0x08)
						{
							ucBypassedOne = 1;
						}
                        break;
                    }
                    else if(nRet == EMV_PIN_TRYAGAIN)
                    {
                        nTryFlg =1;
                        ucPINCnt = ucPINTryCount;
                        memset(aucPINData,0,sizeof(aucPINData));
                        continue;
                    }
                    else if (nRet == EMV_PED_FAIL)
                    {
                        aucPINData[0] = aucPINForamt[0];
                        continue;
                    }
					else if (nRet == EMV_USER_CLEAR)
					{
						return EMV_USER_CLEAR;
					}
                }
                else
                {
                }
                break;
            }
        }
        if(nRet != EMV_NEXT_CVM)
            return nRet;
    }
    return 0;
}

Context环境类:环境类又称为上下文类,它是拥有多种状态的对象。
由于环境类的状态存在多样性且不同状态下对象的行为有所不同,
因此将状态独立出去形成独立的状态类。在环境类中维护一个抽象
状态类State的实例,这个实例定义为当前状态,在具体实现时,
它是一个State子类的对象。

抽象状态类:
它用于定义一个接口封装与环境类的一个特定状态相关的行为,
在抽象状态类中声明了各种不同状态对应的方法,而在其子类中
实现类这些方法,由于不同状态下对象的行为可能不同,
因此在不同子类中方法的实现可能存在不同,相同方法可以写在抽象
状态类中。

ConcreteState具体状态类
它是抽象状态类的子类,每一个子类实现一个与环境类的一个状态
相关的行为,每一个具体状态类对应环境的一个具体状态,不同的具体
状态类其行为有所不同。

在状态模式中,我们将对象在不同状态下的行为封装到不同的状态类中,
为了让系统具有更好的灵活性和可扩展性,同时对各状态下共有行为
进行封装,我们需要对状态进行抽象,引入抽象状态类角色。

1.调用整个逻辑

public boolean processCVM() {
        ContextCVM contextCVM = new ContextCVM();
        contextCVM.setState(new StartCVMState(contextCVM));
        try {
            while (true) {
                contextCVM.executeCVM();
                // 在这里判断是否退出循环
                if (contextCVM.getState() instanceof QuitCVMState) {
                    // Logic for quitting CVM
                    Logs.d(TAG,"QuitCVMState");
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

2.抽象状态类

//抽象状态类
public abstract class CVMState {
    protected ContextCVM contextCVM;

    public CVMState(ContextCVM contextCVM) {
        this.contextCVM = contextCVM;
    }

    public abstract void executeCVM();
}

3.环境类

//环境类
public class ContextCVM {
    private CVMState state; //维持一个对抽象状态对象的引用
    private ByteArray cvmType = new ByteArray();
    private ByteArray pinCnt = new ByteArray();
    private int bypassedOne;
    private int PwdRst;
    private int PINCancelFlag;
    EMV_TERMPARAM tTermParam ;

    public ContextCVM() {
        // 初始化状态等
        this.state = new StartCVMState(this);
        this.cvmType = new ByteArray();
        this.pinCnt = new ByteArray();
        Arrays.fill(cvmType.data, (byte) 0x00);
        Arrays.fill(cvmType.data, (byte) 0x00);
        this.bypassedOne = 0;
        this.PwdRst = 0;
        this.PINCancelFlag = 0;
        tTermParam = new EMV_TERMPARAM();
    }

    public void executeCVM() {
        state.executeCVM();
    }

    public int getPINCancelFlag() {
        return PINCancelFlag;
    }

    public void setPINCancelFlag(int PINCancelFlag) {
        this.PINCancelFlag = PINCancelFlag;
    }

    public int getPwdRst() {
        return PwdRst;
    }

    public void setPwdRst(int pwdRst) {
        PwdRst = pwdRst;
    }

    public int getBypassedOne() {
        return bypassedOne;
    }

    public void setBypassedOne(int bypassedOne) {
        this.bypassedOne = bypassedOne;
    }

    public EMV_TERMPARAM gettTermParam() {
        EMV_GetTermParam(tTermParam);
        return tTermParam;
    }

    public boolean cvmGetPin(){
        return true;
    }

    public CVMState getCurrentState() {
        return state;
    }

    public CVMState getState() {
        return state;
    }

    public void setState(CVMState state) {
        this.state = state;
    }

    public void setCvmType(ByteArray cvmType) {
        this.cvmType = cvmType;
    }

    public ByteArray getCvmType() {
        return cvmType;
    }



    public ByteArray getPinCnt() {
        return pinCnt;
    }

    public void setPinCnt(ByteArray pinCnt) {
        this.pinCnt = pinCnt;
    }


}

4.具体抽象类

public class OnlinePINState extends CVMState {
    static final String TAG = OnlinePINState.class.getSimpleName();
    private int ret = 0;
    public OnlinePINState(ContextCVM contextCVM) {
        super(contextCVM);
    }
    @Override
    public void executeCVM() {
        int ucBypassedOne = contextCVM.getBypassedOne();
        EMV_TERMPARAM tTermParam = contextCVM.gettTermParam();
        ByteArray cvmType = contextCVM.getCvmType();
        ByteArray pinCnt = contextCVM.getPinCnt();
        if (ucBypassedOne == 1 && tTermParam.bypassAllFlg == 1) {
            contextCVM.setState(new NextCVMState(contextCVM));
        } else {
            UiNotifyPINEntryData uiNotifyPINEntryData = new UiNotifyPINEntryData(1,"Input online Pin",12,12);
            EMVTransactionExecutor.INSTANCE.notifyObservers(uiNotifyPINEntryData);
            Logs.d(TAG,"input online Pin!");
            byte[] pinBlock = null;
            try {
                pinBlock = PedManager.getInstance().pedGetPinBlock((byte) 1, "0,4,6",
                        tools.hexStringToByte("4551820000009478"), (byte) 1, 3000);
            } catch (PedException e) {
                throw new RuntimeException(e);
            }
            GetPINOutput getPINOutput = new GetPINOutput();
            getPINOutput.setPinBlock(tools.hexString(pinBlock));
            getPINOutput.setReturnCode(ReturnCodes.OK); // Set the appropriate return code
            EMVTransactionExecutor.INSTANCE.notifyObservers(getPINOutput);
            contextCVM.setPwdRst(0);
            ret = EMV_CompleteCVM(0, cvmType, pinCnt);
            if (ret == EMV_NEXT_CVM) {
                if (contextCVM.getPwdRst() == EMV_NO_PASSWORD) {
                    contextCVM.setBypassedOne(1);
                    contextCVM.setState(new NextCVMState(contextCVM));
                }
            } else if (ret == EMV_USER_CLEAR) {
                // Logic for user clearing
                Logs.d(TAG,"EMV_USER_CLEAR ret = "+ ret );
                contextCVM.setState(new QuitCVMState(contextCVM));
            }else {
                contextCVM.setState(new QuitCVMState(contextCVM));
            }
        }
    }
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值