[英]C++ inheritance and abstract class
I have an abstract class Interface
. 我有一个抽象类
Interface
。 An interface has a read
method that reads the data and parses it, and a getData
method that actually returns the parsed data. 接口具有
read
方法并读取数据并对其进行解析,而getData
方法则实际返回已解析的数据。 Each interface has a Parser
object that does the actual parsing. 每个接口都有一个
Parser
对象,可以执行实际的解析。 A Parser has a parse
method and getters for the parsed data. 解析器具有
parse
方法和用于解析数据的吸气剂。
The classes USBInterface
and SerialInterface
inherit from Interface
. 该班
USBInterface
和SerialInterface
继承Interface
。
The problem: 问题:
How can I use a different parser for USBInterface
and SerialInterface
? 我如何可以使用不同的解析器
USBInterface
和SerialInterface
?
There are two parsers, USBParser
and SerialParser
, that inherit from Parser
. 有两个解析器,
USBParser
和SerialParser
,它们继承自Parser
。
My current solution uses a Parser
reference in the Interface
base class that's initialized in the Interface constructor, but I'm not sure if this is the best approach. 我当前的解决方案在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());
}
Are there any flaws in my approach, or is there a better way? 我的方法是否有缺陷,或者有更好的方法?
Your code looks bit over-complicated to me. 您的代码对我来说有点复杂。 Instead you can use dependency injection as class
USBInterface
depends on USBParser
like wise SerialInterface
depends on SerialParser
. 相反,你可以使用依赖注入的类
USBInterface
取决于USBParser
像明智SerialInterface
取决于SerialParser
。 It gives you more flexibility. 它给您更多的灵活性。 In case you have something like
GenericParser
that you want to use with USBInterface
or SerialInterface
. 如果你有类似
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.