[英]C++ inheritance and abstract class
我有一個抽象類Interface
。 接口具有read
方法並讀取數據並對其進行解析,而getData
方法則實際返回已解析的數據。 每個接口都有一個Parser
對象,可以執行實際的解析。 解析器具有parse
方法和用於解析數據的吸氣劑。
該班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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.