Solution: Abstract Factory

I2CSensorFactory Abstract Factory

#pragma once

#include <sensor-i2c.h>


class I2CSensorFactory
{
public:
    virtual ~I2CSensorFactory() {}
    virtual I2CSensor* create(unsigned int bus, uint8_t address) = 0;
};

Concrete Factory Implementations

I2CSensorFactory_Vendor1

#pragma once

#include "sensor-i2c-factory.h"

class I2CSensorFactory_Vendor1 : public I2CSensorFactory
{
public:
    virtual I2CSensor* create(unsigned int bus, uint8_t address) override;
};
#include "sensor-i2c-factory-vendor1.h"

#include "sensor-i2c-vendor1.h"


I2CSensor* I2CSensorFactory_Vendor1::create(unsigned int bus, uint8_t address)
{
    return new I2CSensor_Vendor1(bus, address);
}

I2CSensorFactory_Vendor1

#pragma once

#include "sensor-i2c-factory.h"

class I2CSensorFactory_Vendor2 : public I2CSensorFactory
{
public:
    virtual I2CSensor* create(unsigned int bus, uint8_t address) override;
};
#include "sensor-i2c-factory-vendor2.h"

#include "sensor-i2c-vendor2.h"


I2CSensor* I2CSensorFactory_Vendor2::create(unsigned int bus, uint8_t address)
{
    return new I2CSensor_Vendor2(bus, address);
}