lundi 28 décembre 2015

Design pattern for device data link

I have three types of devices (USB, COM and wireless) and one PC software to connect with them. Every device have functions like connect, read and write data from RS485 network. On PC software I must implement application layer classes to work with devices. I am looking for some design pattern to write connection between application layer and transport layer. The first idea is to write abstract class DataLink and every device will inherit from the abstract class interface (pure OOP):

class DataLink {
    public:
    virtual bool read() = 0;
    virtual bool write() = 0;
};

class USBDevice : public DataLink {
    public:
    bool read() { /* some code */ }
    bool write() { /* some code */ }
    bool specificUSBFunction() { /* some code */ }
};

class COMDevice : public DataLink {
    public:
    bool read() { /* some code */ }
    bool write() { /* some code */ }
    bool specificCOMFunction(){ /* some code */ } 
};

DataLink *dl = new COMDevice();
dl->read();
dl->write();

Now if I want to use specific USB or COM function I must use ugly cast. The other problem is that this class must be singleton because we have only one device available so we cannot create multiple objects. I am looking for a good way to do this using C++ (can be v11 or v14).

Aucun commentaire:

Enregistrer un commentaire