典型的适配者模式图在此省略。
APP中需要驱动接口,那么需要进行适配,
方法1:基类实现虚接口,应用的子类中实现具体接口。
1)CBB的目标接口为虚基类:
class RadarDeviceMcuTarget
{
public :
RadarDeviceMcuTarget(){}
virtual ~RadarDeviceMcuTarget(){}
virtual UINT32 SpidevTxRxDataWithDma(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block ) = 0;
virtual RadarDeviceMcuTarget* CreateMcuTarget() = 0;//多态指向实现级实例
}
2)CBB使用:
class RadarDevicePrivateData;
class RadarDevice
{
public :
RadarDevice(RadarDeviceProperty& property, RadarDeviceMcuTarget* mcuTarget);
virtual ~RadarDevice(){}
public :
UINT32 RadarSpidevOperate(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block ) ;
private:
RadarDevicePrivateData* m_PrivateData;
}
RadarDevice::RadarDevice(RadarDeviceProperty& property, RadarDeviceMcuTarget* mcuTarget)
m_PrivateData(new RadarDevicePrivateData)
{
m_PrivateData->m_Property = property;
m_PrivateData->m_ McuTarget = mcuTarget->CreateMcuTarget() ;
}
UINT32 RadarDevice::RadarSpidevOperate(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block )
{
return m_PrivateData->m_ McuTarget->SpidevTxRxDataWithDma(wbuf, rbuf, len, block ) ;
}}
class RadarDevicePrivateData
{
public :
RadarDevicePrivateData(){}
virtual ~RadarDevicePrivateData(){}
public :
RadarDeviceProperty m_Property;
RadarDeviceMcuTarget* m_ McuTarget;
}
3)APP实现适配子类:
class RadarDeviceMcuTargetAdapter :public RadarDeviceMcuTarget
{
RadarDeviceMcuTarget(){}
~RadarDeviceMcuTarget(){}
UINT32 SpidevTxRxDataWithDma(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block ) ;
RadarDeviceMcuTarget* CreateMcuTarget();//多态指向实现级实例
}
UINT32 RadarDeviceMcuTargetAdapter ::SpidevTxRxDataWithDma(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block )
{
//关联到McuDeviceAdaptee,由驱动实现
}
RadarDeviceMcuTarget* RadarDeviceMcuTargetAdapter ::CreateMcuTarget()
{
return new RadarDeviceMcuTargetAdapter ;
}
方法2:CBB中接口只定义H,应用实现CPP,CBB中直接使用单例的接口
1)CBB的H文件定义接口:
class RadarDeviceMcuTarget
{
public :
RadarDeviceMcuTarget(){}
virtual ~RadarDeviceMcuTarget(){}
static RadarDeviceMcuTarget& Instance();//单例应用
UINT32 SpidevTxRxDataWithDma(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block ) ;
}
2)应用CPP中实现
RadarDeviceMcuTarget& RadarDeviceMcuTarget::Instance()
{
static RadarDeviceMcuTarget sInstance;
return sInstance;
}
UINT32 RadarDeviceMcuTarget::SpidevTxRxDataWithDma(UCHAR *wbuf, UCHAR *rbuf, UINT32 len, BOOL block )
{
}