I am working on a library that defines a client interface for some service. Under the hood, I have to check the data provided by users, and then transfer it to the “engine” process using the Connection class from another library (note: the Connection class is not known to users of our library). One of my colleagues suggested using PIMPL:
class Client {
public:
Client();
void sendStuff(const Stuff &stuff) {_pimpl->sendStuff(stuff);}
Stuff getStuff(const StuffId &id) {return _pimpl->getStuff(id);}
private:
ClientImpl *_pimpl;
}
class ClientImpl {
public:
void sendStuff(const Stuff &stuff);
Stuff getStuff(const StuffId &id);
private:
Connection _connection;
}
However, it’s very difficult for me to test - even if I associate my tests with some ridiculous implementation of Connection, I don’t have easy access to it for setting and checking expectations. I missed something, or the + factory interface is used for a cleaner and more tested solution:
class ClientInterface {
public:
void sendStuff(const Stuff &stuff) = 0;
Stuff getStuff(const StuffId &id) = 0;
}
class ClientImplementation : public ClientInterface {
public:
ClientImplementation(Connection *connection);
}
class ClientFactory {
static ClientInterface *create();
}
Are there any reasons for using PIMPL in this situation?