Dongle烧写模块重构(二)--让方案商直接面对接口编程

用继承的方式来给Dongle模块做一个框架不能保证良好的拓展性,因为对dongle的操作行为和方案商绑定在了一起。

为了让新的方案商加入时不去考虑哪些自己该覆盖,哪些接口不需要覆盖,可以尝试用接口来处理方案商提供的api

然后,所有的方案商都继承这些接口来实现api就行了,这样的话,可以公司自己定制接口,然后将接口发布给方案商去实现,也便于双方的合作。

比如scanDongle,getDongleInfo这些必需的接口可以要求方案商强制实现。而setDongleToMode这些可选接口可由方案商选择性实现:

 

参考一个别人的文章表述这种设计:

C++中抽象类和接口的区别介绍

C++中是没有接口这个语法上的概念,所以这种面向接口编程思想是通过纯虚基类来实现的

首先看方案商直接面对接口编程:

各个方案商必须实现的一些通用接口放在这里:

class BasePolicy{
public:
	BasePolicy();
	~BasePolicy();
	virtual void scanDongle() = 0;
	virtual void getDongleInfo()=0;
};

方案商Iflytek的接口:

class PolicyIflytek : public BasePolicy {
public:
	PolicyIflytek();
	~PolicyIflytek();
    virtual void setDongleToNomalMode() = 0;
};

方案商Nanosic的接口:

class PolicyNanosic : public BasePolicy {
public:
	PolicyNanosic();
	~PolicyNanosic();
	virtual void setDongleToIspMode() = 0;
};


方案商Iflytek:需要实现指定策略PolicyIflytek的所有接口

class VendorIflytek : public BaseVendor,public PolicyIflytek{
public:
	VendorIflytek();
	~VendorIflytek();
	
	void scanDongle();
	void getDongleInfo();
	void setDongleToNomalMode();
};

方案商Nanosic:需要实现指定策略PolicyNanosic的所有接口

class VendorNanosic : public BaseVendor,PolicyNanosic{
public:
	VendorNanosic();
	~VendorNanosic();
	void scanDongle();
	void getDongleInfo();
	void setDongleToIspMode();
};

测试代码:

	//create a vendor
	BaseVendor *vendor = new VendorIflytek();
	//excute a vendor behavior
	vendor->scanDongle();
	
	//create a vendor
	VendorNanosic *nanosic = new VendorNanosic();
	//excute a vendor behavior
	nanosic->setDongleMode(1);


运行结果:


这里注意的是,因为所有的接口都定义成纯虚函数的形式,所以如果方案商有一部分接口没有实现,是不被允许的,比如方案商Nanosic有一个通用接口getDongleInfo没有实现:

#include "VendorNanosic.h"

VendorNanosic::VendorNanosic(){
	LOGE("VendorNanosic::VendorNanosic");

}
VendorNanosic::~VendorNanosic(){
	LOGE("VendorNanosic::~VendorNanosic");
}
void VendorNanosic::scanDongle(){
	LOGE("VendorNanosic::scanDongle");
}
/*这个接口的实现注释掉不实现,是不行的(头文件中也注释掉)
void VendorNanosic::getDongleInfo(){
	LOGE("VendorNanosic::getDongleInfo");
}
*/
void VendorNanosic::setDongleMode(int mode){
	LOGE("VendorNanosic::setDongleMode,mode = %d",mode);
	if(mode == 1)
	{
		iSetDongleMode = new SetDongleToIspMode();
	}else{
		iSetDongleMode = new SetDongleToNormalMode();
	}
	iSetDongleMode->setDongleMode();
}

就不可以,会提示还有纯虚函数没有具体实现:

AndroidPeripheralAssist/jni/main.cpp: In function 'int main(int, char**)':
AndroidPeripheralAssist/jni/main.cpp:73:45: error: cannot allocate an object of abstract type 'VendorNanosic'
AndroidPeripheralAssist/jni/vendors/nanosic/VendorNanosic.h:13:7: note:   because the following virtual functions are pure within 'VendorNanosic':
AndroidPeripheralAssist/jni/vendors/../policys/BasePolicy.h:14:15: note:      virtual void BasePolicy::getDongleInfo()
AndroidPeripheralAssist/jni/vendors/BaseVendor.h:18:15: note:         virtual void BaseVendor::getDongleInfo()
AndroidPeripheralAssist/obj/local/armeabi/objs/android_peripheral_assist/main.o] Error 1


这样用接口,策略,方案商三个体系分析来设计的话,各个方案商就只需要针对给出的策略(即接口集)来进行编程,且可以达到强制方案商必须实现指定的某些接口的目的


不过这样的话,会出现一个问题,每一个接口表示一种行为,而如果方案商对这一行为有好几种实现方式,还不能让方案商将其针对每个接口的多种实现都方便地纳入到框架中来呢?这个下一篇博文会进行处理。


Introduction The nRF Sniffer is a tool for debugging Bluetooth low energy (BLE) applications by detecting packets between a selected device and the device it is communicating with, even when the link is encrypted. When developing a BLE product, knowing what happens over-the-air between devices can help you isolate and solve any potential issues. By default, the Sniffer lists nearby BLE devices that are advertising, providing the Bluetooth Address and Address type, complete or shortened name, and RSSI. 1.1 Required hardware To set up the Sniffer you will need one of the following kits: • nRF51 Development Kit (PCA10028) v1.0 or later and a micro USB cable • nRF51 Dongle (PCA10031) • nRF51822 Evaluation Kit (PCA10001) and a mini USB cable • nRF51422 Evaluation Kit (PCA10003) v3.0.0 or later and a mini USB cable • nRF51822 Development Kit dongle (PCA10000) • nRF52 Development Kit (PCA10040) and a micro USB cable • nRF52840 Development Kit (PCA10056) and a micro USB cable 1.2 Required software • nRF Sniffer software v2.x or later available on the Sniffer product page under the downloads tab. This also includes the SEGGER J-Link software that is compatible. • Wireshark v2.4.6 or later available from http://www.wireshark.org/. Wireshark is a free software tool that captures wireless traffic and reproduces it in a readable format. • An operating system that runs the required version of Wireshark • Windows 7 or later • 64 bit OS X/macOS 10.6 or later • Linux (check for version compatibility) • python v2.7.x available from https://www.python.org/downloads/ • pyserial v3.4 or later available from https://github.com/pyserial/pyserial • Type “pip --version” in the terminal to verify that the pip installed with python v2.7 is used • For Windows - “C:\Python27\Scripts\pip.exe install -r requirements.txt” to install the python modules required for nRF Sniffer v2 • For OS X/mac OS/Linux - verify that the pip software being used is the pip installed with python 2.7. Then type “pip install -r requirements.txt” into the terminal 1.3 Writing conventions This user guide follows a set of typographic rules that make the document consistent and easy to read. The following writing conventions are used: • Commands are written in Lucida Console. • Pin names are written in Consolas. • File names and User Interface components are written in bold. • Internal cross-references are italicized and written in semi-bold.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值