Wie implementiert man das Factory Method Pattern in C++ für Embedded-Anwendungen?

Hier ein einfaches Beispiel:

class Sensor {
public:
    virtual void read() = 0;
    virtual ~Sensor() {}
};

class I2CSensor : public Sensor {
public:
    void read() override { /* I2C read logic */ }
};

class SPISensor : public Sensor {
public:
    void read() override { /* SPI read logic */ }
};

class SensorFactory {
public:
    static Sensor* createSensor(const std::string& type) {
        if (type == "I2C") return new I2CSensor();
        if (type == "SPI") return new SPISensor();
        return nullptr;
    }
};
com

Newsletter Anmeldung

Bleiben Sie informiert! Wir informieren Sie über alle neuen Beiträge (max. 1 Mail pro Woche – versprochen)