C++继承和抽象类
C++ inheritance and abstract class
我有一个抽象类Interface
。接口具有读取数据并对其进行分析的read
方法,以及实际返回已分析数据的getData
方法。每个接口都有一个执行实际解析的Parser
对象。解析器具有parse
方法和解析数据的 getter。
类USBInterface
和SerialInterface
继承自Interface
。
问题:
如何使用不同的解析器进行USBInterface
和SerialInterface
?
有两个解析器,USBParser
和SerialParser
,继承自Parser
。
我当前的解决方案在 Interface 构造函数中初始化的 Interface
基类中使用Parser
引用,但我不确定这是否是最佳方法。
class Parser {
public:
Parser() {}
int getData() {
return data;
}
protected:
int data;
};
class USBParser : public Parser {
public:
USBParser() {}
bool parse(uint8_t *usbpacket) {
data = usbpacket[1]; // Do the actual parsing here
return true; // Return true if data is complete
}
};
class SerialParser : public Parser {
public:
SerialParser() {}
bool parse(uint8_t databyte) {
data = databyte; // Do the actual parsing here
return true; // Return true if data is complete
}
};
class Interface {
public:
Interface(Parser &parser) : parser(parser) {}
virtual bool read() = 0;
int getData() {
return parser.getData();
}
protected:
Parser &parser;
};
class USBInterface : public Interface {
public:
USBInterface() : Interface(parser) {}
bool read() {
uint8_t usbpacket[4] = {0x00, 0x01, 0x02, 0x03}; // Read raw data from USB
return parser.parse(usbpacket);
}
private:
USBParser parser;
};
class SerialInterface : public Interface {
public:
SerialInterface() : Interface(parser) {}
bool read() {
uint8_t databyte = 0xFF; // Read raw data from serial port
return parser.parse(databyte);
}
private:
SerialParser parser;
};
int main() {
USBInterface usb;
SerialInterface serial;
if (usb.read())
println(usb.getData());
if (serial.read())
println(serial.getData());
}
我的方法是否有任何缺陷,或者有更好的方法?
你的代码对我来说看起来有点过于复杂了。相反,您可以使用依赖注入作为类USBInterface
取决于USBParser
,就像明智的SerialInterface
取决于SerialParser
。它为您提供了更大的灵活性。如果您有类似GenericParser
的东西,您想与 USBInterface
或 SerialInterface
.
class Parser {
public:
Parser() {}
int getData() {
return data;
}
protected:
int data;
};
class USBParser : public Parser {
public:
USBParser() {}
bool parse(uint8_t *usbpacket) {
data = usbpacket[1]; // Do the actual parsing here
return true; // Return true if data is complete
}
};
class SerialParser : public Parser {
public:
SerialParser() {}
bool parse(uint8_t databyte) {
data = databyte; // Do the actual parsing here
return true; // Return true if data is complete
}
};
class Interface {
public:
Interface(Parser &parser) : parser(parser) {}
virtual bool read() = 0;
int getData() {
return parser.getData();
}
protected:
Parser &parser;
};
class USBInterface : public Interface {
public:
USBInterface(Parser &parser) : Interface(parser) {}
bool read() {
uint8_t usbpacket[4] = {0x00, 0x01, 0x02, 0x03}; // Read raw data from USB
return parser.parse(usbpacket);
}
};
class SerialInterface : public Interface {
public:
SerialInterface(Parser &parser) : Interface(parser) {}
bool read() {
uint8_t databyte = 0xFF; // Read raw data from serial port
return parser.parse(databyte);
}
};
int main() {
USBParser usbParse
USBInterface usb(usbParse);
SerialParse serialParse;
SerialInterface serial(serialParse);
// GenericParse parse;
// SerialInterface serial(parse);
if (usb.read())
println(usb.getData());
if (serial.read())
println(serial.getData());
}
相关文章:
- 从抽象类继承以创建另一个抽象类时,我应该重新声明所有虚函数吗?
- 抽象类析构函数与继承"Shutdown"函数
- C++的抽象类继承和构造函数的问题
- C++:抽象类继承和构造
- 抽象类 A 由类 C 继承"through"类 B "intermediary"。如何声明虚拟功能?
- 如何修复抽象类的迭代继承
- 具有继承类和函数的抽象类写入单独的向量c++
- 继承自C++中的抽象类
- C++抽象类和继承
- 从抽象类继承的类的实例
- googlemock:遗留代码类不是从抽象类继承的
- 在从抽象类继承的模板类中正确使用未定义的类型
- 从抽象类继承方法
- c'tor 和 d'tor 从抽象基础通过其他抽象类继承到具体
- 从抽象类继承的抽象方法是否存在任何性能问题
- 从两个抽象类继承
- 将抽象类继承与实例化模板混合
- c++抽象类继承
- 抽象类继承
- 从抽象类C++继承的对象的数组