C ++继承和抽象类

时间:2018-03-18 13:31:11

标签: c++ oop inheritance abstract-class

我有一个抽象类Interface。接口具有读取数据并解析数据的read方法,以及实际返回解析数据的getData方法。每个接口都有一个Parser对象,可以进行实际的解析。解析器具有parse方法和解析数据的getter 类USBInterfaceSerialInterface继承自Interface

问题:
如何为USBInterfaceSerialInterface使用不同的解析器? 有两个解析器USBParserSerialParser,它们继承自Parser

我当前的解决方案在接口构造函数中初始化的Parser基类中使用Interface引用,但我不确定这是否是最好的方法。

 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());
}

我的方法有什么缺陷,还是有更好的方法?

1 个答案:

答案 0 :(得分:0)

你的代码看起来有点过于复杂了。相反,您可以使用依赖注入,因为类USBInterface依赖于USBParser,明智的SerialInterface取决于SerialParser。它为您提供更多灵活性。如果您想要GenericParserUSBInterface使用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());
}