Is this a good place to use the PIMPL template?

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 { // not exported
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 { // not exported
public:
    ClientImplementation(Connection *connection);
    // +implementation of ClientInterface
}

class ClientFactory {
    static ClientInterface *create();
}

Are there any reasons for using PIMPL in this situation?

+5
3

AFAIK Pimpl / ( ). , (, ).

, , , + a factory. , , unit test, .

+4
+1

, Pimpl, , , .

, :

  • Pimpl - : / ABI.
  • (, )

, . , .

, , Connection ?

class Connection
{
private:
  ConnectionImpl* mImpl;
};

Factory:

// Production code:

Client client = factory.GetClient();

// Test code:
MyTestConnectionImpl impl;
Client client = factory.GetClient(impl);

, , ABI.

0

All Articles