Design: Remove dependency on data structure exchanged between interfaces

Viewed 59

I have three interfaces (abstract classes as I use C++): three services. And it's all great: I use mocks to unit-test them and their communication.

The problem is that I have realized that each of the interfaces nails down what data struct it does send to its service peer, e.g. (very roughly speaking):

class ServiceAInterface {
public:
    struct Data
    {
        QString field1; // Implementation details are here. So sad.
        int field2;
    };

public:
signal:
    void notifyServiceB(const Data* data);
};

How can I remove this dependency between my services?

  1. The first solution that comes to my mind is to have the Data as an interface. But the problem is that theoretically my services can have different implementations that would use different data structures. E.g., today it returns a QJsonObject and tomorrow it's some Xml or something. So the data does not have a common interface.

  2. I could also have something like this:

struct Data;

class ServiceAInterface {
public:
signal:
    void notifyServiceB(const Data* data);
};

But then if I have different implementations for ServiceAInterface: ServiceAImpl1 and ServiceAImpl2, how can I have different Data for them?

  1. The last solution to which I have come to is to have a template abstract class:
template<typename T>
class ServiceAInterface {
public:
signal:
    void notifyServiceB(const T* data);
};

But it also will not work, as ServiceAInterface<Data1> and ServiceAInterface<Data2> will be two different base class for the two implementations.


Is there any common pattern on how to remove the dependency on data structure exchanged between interfaces? Thank you.

0 Answers
Related