簡體   English   中英

C ++繼承和抽象類

[英]C++ inheritance and abstract class

我有一個抽象類Interface 接口具有read方法並讀取數據並對其進行解析,而getData方法則實際返回已解析的數據。 每個接口都有一個Parser對象,可以執行實際的解析。 解析器具有parse方法和用於解析數據的吸氣劑。
該班USBInterfaceSerialInterface繼承Interface

問題:
我如何可以使用不同的解析器USBInterfaceSerialInterface
有兩個解析器, USBParserSerialParser ,它們繼承自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要與使用USBInterfaceSerialInterface

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM